Ben,
That’s very clear.. Would you mind making a Pull Request with this change?
--
Arun
On 19 Feb 2015 at 15:49:16, Ben Tupper ([email protected]) wrote:
Hi,
On Feb 18, 2015, at 12:11 PM, Arunkumar Srinivasan <[email protected]>
wrote:
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”]].
Thanks for this; the DT[['col']] suites my needs perfectly. I can see in the
examples (for ?data.table) and in the FAQ the examples that show the column
selection behavior using 'with'. This discussion makes me wonder if the
documentation for 'with' might benefit from a small embellishment. Perhaps
like this?
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 character vector of column names
or a numeric vector of column positions to select, and the value returned is
always a data.table. with=FALSE is often useful in data.table to select
columns dynamically.
I think the above faithfully describes the behavior I see, but I defer to you
to know what is best.
Thanks again,
Ben
--
Arun
On 12 Feb 2015 at 22:28:08, Ben Tupper ([email protected]) 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
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
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
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help_______________________________________________
datatable-help mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help