<html><head><style>body{font-family:Helvetica,Arial;font-size:13px}</style></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">Please go through the FAQ. This is outlined in the data.table FAQ 2.17 - smaller syntax differences between data.frame and data.table.</div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;"><br></div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">DT[, col] returns a vector because there’s no other way to return a vector and users wanted a way to return a vector.</div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">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.</div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">‘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”]].</div> <br> <div id="bloop_sign_1424279289275484928" class="bloop_sign"><div style="font-family:helvetica,arial;font-size:13px">-- <br>Arun</div></div> <br><p style="color:#000;">On 12 Feb 2015 at 22:28:08, Ben Tupper (<a href="mailto:btupper@bigelow.org">btupper@bigelow.org</a>) wrote:</p> <blockquote type="cite" class="clean_bq"><span><div><div></div><div>Hello,
<br>
<br>I would like to extract a column of a data.table, but I get unexpected (to me) results when I specify a column dynamically.
<br>
<br>DT <- data.table(ID = c("b","b","b","a","a","c"), a = 1:6, b = 7:12, c = 13:18)
<br>thisone = "a"
<br>
<br>str(DT[,a])
<br># int [1:6] 1 2 3 4 5 6
<br>
<br>str(DT[,"a", with = FALSE])
<br># Classes ‘data.table’ and 'data.frame': 6 obs. of 1 variable:
<br># $ a: int 1 2 3 4 5 6
<br># - attr(*, ".internal.selfref")=<externalptr>
<br>
<br>str(DT[, thisone, with = FALSE])
<br># Classes ‘data.table’ and 'data.frame': 6 obs. of 1 variable:
<br># $ a: int 1 2 3 4 5 6
<br># - attr(*, ".internal.selfref")=<externalptr>
<br>
<br>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.
<br>
<br>"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."
<br>
<br>How should I extract a single column dynamically to retrieve a vector?
<br>
<br>Cheers and thanks,
<br>Ben
<br>
<br>> sessionInfo()
<br>R version 3.1.0 (2014-04-10)
<br>Platform: x86_64-apple-darwin13.1.0 (64-bit)
<br>
<br>locale:
<br>[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
<br>
<br>attached base packages:
<br>[1] stats graphics grDevices utils datasets methods base
<br>
<br>other attached packages:
<br>[1] data.table_1.9.5 devtools_1.6.1
<br>
<br>loaded via a namespace (and not attached):
<br>[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
<br>[8] tools_3.1.0
<br>
<br>Ben Tupper
<br>Bigelow Laboratory for Ocean Sciences
<br>60 Bigelow Drive, P.O. Box 380
<br>East Boothbay, Maine 04544
<br>http://www.bigelow.org
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>_______________________________________________
<br>datatable-help mailing list
<br>datatable-help@lists.r-forge.r-project.org
<br>https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
<br></div></div></span></blockquote></body></html>