Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: setDT for matrices #6565

Open
iago-pssjd opened this issue Oct 10, 2024 · 4 comments
Open

FR: setDT for matrices #6565

iago-pssjd opened this issue Oct 10, 2024 · 4 comments

Comments

@iago-pssjd
Copy link
Contributor

Currently, setDT accepts as main argument

x: A named or unnamedlist’, ‘data.frameordata.table

Would it have sense to accept a matrix too instead of asignment dt <- as.data.table(matrix(...))?

@ecoRoland2
Copy link

I don't think that makes sense. A data.frame and a data.table are lists. A matrix is a very different data structure. How often do you create data.tables from matrices that are so large that you need to avoid a deep copy?

@ChristK
Copy link

ChristK commented Oct 10, 2024

@ecoRoland2 Leaving implementation difficulties aside, I often do simulations using Rcpp and matrices, as matrices are much more intuitive to work with on the C++ side. The outputs are also matrices, sometimes several GB each. Having a more efficient way to convert a matrix to a data.table on the R side would be nice. So, the request by @iago-pssjd is not outrageous, and there are real-life use cases, albeit perhaps rare.

@MichaelChirico
Copy link
Member

I think you'll have to do this yourself, like

DT=data.table(M[, 1])
# maybe setalloccol() if M is very wide
for (j in 2:ncol(M))
  set(DT,,j, M[, j])
setnames(DT, colnames(M))

this could conceivably be wrapped into a data.table export, though I worry about the troubles of creating a column-by-column by-reference copy of an array.

it will definitely not be part of setDT, the semantics are way too different.

@ben-schwen
Copy link
Member

ben-schwen commented Oct 12, 2024

AFAIR there is a PR for a fast matrix to data.table conversion (edit: it is #4144)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants