[datatable-help] Construct a new data table from another

Arunkumar Srinivasan aragorn168b at gmail.com
Mon Jul 21 17:02:50 CEST 2014


Hi Frank,

The data.frame way of referring to column names or numbers requires with=FALSE. This is because the default data.table-like operations are more common where with=TRUE. Also, data.tables are designed with really huge data sets in mind and avoiding as many copies as possible. So, unless there’s a strong reason, the data.table philosophy would be to avoid copies (ex: DT[, 1, with=FALSE] will create a copy).

That being said, another alternative is to subset the data.table way (where with=TRUE by default):

DT2 = DT[, list(A,D,B)] # list(1,4,2) won't work. Read FAQ 1.1-1.5
setnames(DT2, c("a", "d", "B"))
It’s also generally considered a bad practice to subset columns by using column numbers - prone to errors.

Hope this helps.


Arun

From: Frank S. f_j_rod at hotmail.com
Reply: Frank S. f_j_rod at hotmail.com
Date: July 21, 2014 at 4:48:54 PM
To: Chris Neff caneff at gmail.com, datatable-help at lists.r-forge.r-project.org datatable-help at lists.r-forge.r-project.org
Subject:  Re: [datatable-help] Construct a new data table from another  

Thanks Chris,

 

But, is there any option in order to avoid writing "with=FALSE" every time

in the next situation?:

 

 

# USING DATA FRAME 
set.seed(100)
DF = data.frame(A=letters[1:5],B=rnorm(5),C=rexp(5),D=runif(5))
DF2 <- data.frame(a=DF[,1],d=DF[,4],B=DF[,2])

If I do the equivalent operations under data table format, I'm onñy able to obtain
the same result with the following code:
 
# USING DATA TABLE
set.seed(100)
DT = data.table(A=letters[1:5],B=rnorm(5),C=rexp(5),D=runif(5))
DT2 <- data.table(DT[,1,with=FALSE], DT[,4,with=FALSE], DT[,2,with=FALSE])

setnames(DT2, c('a','d','B'))

 

Thank you!

_______________________________________________  
datatable-help mailing list  
datatable-help at lists.r-forge.r-project.org  
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20140721/2772dd42/attachment.html>


More information about the datatable-help mailing list