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

matrix row.name #5315

Closed
Liripo opened this issue Jan 19, 2022 · 1 comment · Fixed by #5364
Closed

matrix row.name #5315

Liripo opened this issue Jan 19, 2022 · 1 comment · Fixed by #5364
Labels

Comments

@Liripo
Copy link

Liripo commented Jan 19, 2022

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) 
 } 
@ben-schwen
Copy link
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
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants