[datatable-help] columns in .SD with grouping ad-hoc using "by"

Arunkumar Srinivasan aragorn168b at gmail.com
Sun May 12 13:54:31 CEST 2013


I just realised that I sent it only to MatthewDowle. So, sending it again. Sorry @Matthew for the double email. 

Matthew,

>> .BY is available to j already for that reason, does that work? .BY isn't a column of .SD because i) it's the same value for every row of .SD i.e. .BY[[1]] is length 1 and contains this particular group (replicating the same value would be wasteful)

DT[, print(.BY), by = list(grp = x %/% 2)]
$grp
[1] 0

$grp
[1] 1

$grp
[1] 2


DT[, print(.SD), by = list(grp = x %/% 2)] # no column "x"
   y
1: 6
   y
1: 7
2: 8
    y
1:  9
2: 10


My question is not as to why the BY column is not available in .SD. Rather, since .BY does not have column "x" in it (rather the result of x%/% 2), why does .SD not have "x"? It's as if grp = x%/%2 is a "new column". So, "x" should be available to .SD is my point.

>> but more significantly  ii) it is often a character group name where running an aggregation function like sum() would trip up on it.

Again, I don't think so because, I am not asking for .BY columns to be in .SD.
DT[, grp := x%/% = 2]
DT[, lapply(.SD, sum), by=grp]

must be equal to:

DT[, lapply(.SD, sum), by = list(grp = x%/%2)] # here, "x" should be available to .SD as it's not the grouping column

Arun

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20130512/cc6ead2e/attachment.html>


More information about the datatable-help mailing list