[datatable-help] extracting columns dynamically

Arunkumar Srinivasan aragorn168b at gmail.com
Wed Feb 18 18:11:40 CET 2015


Please go through the FAQ. This is outlined in the data.table FAQ 2.17 - smaller syntax differences between data.frame and data.table.

DT[, col] returns a vector because there’s no other way to return a vector and users wanted a way to return a vector.
DT[, “col”, with=FALSE] returns a data.table because data.table doesn’t use the ‘drop’ argument. Plus you can always do DT[[“col”]] to subset a column from data.frame/data.tables.
‘drop’ has no other purpose, and it’s default value IMHO is a mistake. I’m not sure about plans to implement it in data.table (I’m not for it). Even if it were, you’d have to do: DT[, “col”, with=FALSE, drop=FALSE] which seems quite bad in comparison to DT[[“col”]].

-- 
Arun

On 12 Feb 2015 at 22:28:08, Ben Tupper (btupper at bigelow.org) wrote:

Hello,  

I would like to extract a column of a data.table, but I get unexpected (to me) results when I specify a column dynamically.  

DT <- data.table(ID = c("b","b","b","a","a","c"), a = 1:6, b = 7:12, c = 13:18)  
thisone = "a"  

str(DT[,a])  
# int [1:6] 1 2 3 4 5 6  

str(DT[,"a", with = FALSE])  
# Classes ‘data.table’ and 'data.frame':	6 obs. of 1 variable:  
# $ a: int 1 2 3 4 5 6  
# - attr(*, ".internal.selfref")=<externalptr>  

str(DT[, thisone, with = FALSE])  
# Classes ‘data.table’ and 'data.frame':	6 obs. of 1 variable:  
# $ a: int 1 2 3 4 5 6  
# - attr(*, ".internal.selfref")=<externalptr>  

I can't noodle out from the help why the latter two don't produce a vector as the first one does. I'm looking at this online resource http://www.rdocumentation.org/packages/data.table/functions/data.table and it doesn't seem like the description of with points to having two different results.  

"with	By default with=TRUE and j is evaluated within the frame of x; column names can be used as variables. When with=FALSE, j is a vector of names or positions to select, similar to a data.frame. with=FALSE is often useful in data.table to select columns dynamically."  

How should I extract a single column dynamically to retrieve a vector?  

Cheers and thanks,  
Ben  

> sessionInfo()  
R version 3.1.0 (2014-04-10)  
Platform: x86_64-apple-darwin13.1.0 (64-bit)  

locale:  
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8  

attached base packages:  
[1] stats graphics grDevices utils datasets methods base  

other attached packages:  
[1] data.table_1.9.5 devtools_1.6.1  

loaded via a namespace (and not attached):  
[1] chron_2.3-45 evaluate_0.5.5 formatR_1.0 httr_0.5 knitr_1.7 RCurl_1.95-4.1 stringr_0.6.2  
[8] tools_3.1.0  

Ben Tupper  
Bigelow Laboratory for Ocean Sciences  
60 Bigelow Drive, P.O. Box 380  
East Boothbay, Maine 04544  
http://www.bigelow.org  








_______________________________________________  
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/20150218/fcc0a8d8/attachment.html>


More information about the datatable-help mailing list