Particularly after performing dcast(), I frequently find myself writing and using the following function to convert a data.table to a matrix:
dt.to.matrix <- function(x) {
x <- as.data.frame(x)
rownames(x) <- x[,1]
x <- as.matrix(x[,-1])
x
}
data.tables do not have a rownames attribute so this information is typically stored as the first column of the data.table. When converting to a matrix it is typically desirable to make this column the rownames() on the matrix. Currently, you have to jump through several hoops to make this conversion following the code above.
This could be taken care of by as.matrix.data.table() itself, e.g. through an additional argument something like as.matrix(dt, rownames = 1), analogous to the keep.rownames argument in as.data.table.
Is there isn't an obvious reason why this is a bad idea (can additional argument be added to S3 methods?) I'm happy to put together and submit a pull request.
Particularly after performing
dcast(), I frequently find myself writing and using the following function to convert adata.tableto a matrix:data.tables do not have arownamesattribute so this information is typically stored as the first column of thedata.table. When converting to amatrixit is typically desirable to make this column therownames()on thematrix. Currently, you have to jump through several hoops to make this conversion following the code above.This could be taken care of by
as.matrix.data.table()itself, e.g. through an additional argument something likeas.matrix(dt, rownames = 1), analogous to thekeep.rownamesargument inas.data.table.Is there isn't an obvious reason why this is a bad idea (can additional argument be added to S3 methods?) I'm happy to put together and submit a pull request.