Skip to content

Allowing rowname specification in as.matrix.data.table  #2692

@sritchie73

Description

@sritchie73

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions