Skip to content

matrix row.name  #5315

Description

@Liripo

data.table/R/fwrite.R

Lines 39 to 42 in eed712e

if (is.matrix(x)) { # coerce to data.table if input object is matrix
messagef("x being coerced from class: matrix to data.table")
x = as.data.table(x)
}

it is possible to output row names when matrix row.names is T ?
for example

 if (is.matrix(x)) { # coerce to data.table if input object is matrix 
   messagef("x being coerced from class: matrix to data.table") 
   x = as.data.table(x,keep.rownames = row.names) 
 } 

Activity

  1. ben-schwen commented on Jan 19, 2022

    @ben-schwen
    Member

    Reprex:

    library(data.table)
    mat = matrix(1:4, nrow=2)
    rownames(mat) = c("a", "b")
    fwrite(mat, row.names=TRUE)
    #> x being coerced from class: matrix to data.table
    #> "",V1,V2
    #> 1,1,3
    #> 2,2,4
    fwrite(as.data.frame(mat), row.names=TRUE)
    #> "",V1,V2
    #> a,1,3
    #> b,2,4
    fwrite(as.data.table(mat, keep.rownames=""), row.names=FALSE)
    #> "",V1,V2
    #> a,1,3
    #> b,2,4

    Setting keep.rownames="" keeps output of data.table and data.frame aligned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions