May I propose a simple way to convert a data.table to a data.frame but keep and move row names? I often have a column named "sample" or some identifier for my data.
I have myself often using data.tables but having to convert them to data.frames or matrices in order to interact and work with other base R functions/code.
I often do this:
dt # a data.table with "rownames" in the sample column
df <- (function(df) {
df <- as.data.frame(df)
rownames(df) <- df$sample
df$sample <- NULL
return(df)
})(dt)
I took some time to study the source code for data.table and found I can modify the method to work as such:
as.data.frame.data.table = function(x, row.names = NULL, ...) {
ans = copy(x)
setattr(ans,"row.names",.set_row_names(nrow(x))) # since R 2.4.0, data.frames can have non-character row names
setattr(ans,"class","data.frame")
setattr(ans,"sorted",NULL) # remove so if you convert to df, do something, and convert back, it is not sorted
setattr(ans,"index",NULL) #4889 #5042
setattr(ans,".internal.selfref",NULL)
# leave tl intact, no harm,
if(!is.null(row.names)) {
rownames(ans) = ans[, row.names]
ans[, row.names] = NULL
}
ans
}
test <- data.table::data.table(iris)
test$sample <- paste("sample", 1:nrow(test), sep = "_")
test2 <- as.data.frame(test, row.names = "sample")
Would this be welcome to the data.table package? I would love to contribute and submit a pull request to data.table!
Edit: this is now a pull request and a bug fix; see PR #5320 allows a user to specify row.names argument as a character vector in as.data.frame.data.table S3 method. Code for as.data.frame.data.table is now simplified and uses setDF on a deep copy of the input. Thanks to @ben-schwen for guiding my contribution.
May I propose a simple way to convert a
data.tableto adata.framebut keep and move row names? I often have a column named "sample" or some identifier for my data.I have myself often using
data.tables but having to convert them todata.framesor matrices in order to interact and work with other base R functions/code.I often do this:
I took some time to study the source code for
data.tableand found I can modify the method to work as such:Would this be welcome to the
data.tablepackage? I would love to contribute and submit a pull request todata.table!Edit: this is now a pull request and a bug fix; see PR #5320 allows a user to specify
row.namesargument as acharactervector inas.data.frame.data.tableS3method. Code foras.data.frame.data.tableis now simplified and usessetDFon a deep copy of the input. Thanks to @ben-schwen for guiding my contribution.