<div>
I just realised that I sent it only to MatthewDowle. So, sending it again. Sorry @Matthew for the double email.
</div><div><br></div><div><div>Matthew,</div><div><br></div><div>>> .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)</div><div><br></div><div>DT[, print(.BY), by = list(grp = x %/% 2)]</div><div><div>$grp</div><div>[1] 0</div><div><br></div><div>$grp</div><div>[1] 1</div><div><br></div><div>$grp</div><div>[1] 2</div></div><div><br></div><div><div>DT[, print(.SD), by = list(grp = x %/% 2)] # no column "x"</div><div><div> y</div><div>1: 6</div><div> y</div><div>1: 7</div><div>2: 8</div><div> y</div><div>1: 9</div><div>2: 10</div></div><div><br></div><div>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.</div><div><br></div><div>>> but more significantly ii) it is often a character group name where running an aggregation function like sum() would trip up on it.</div><div><br></div><div>Again, I don't think so because, I am not asking for .BY columns to be in .SD.</div><div>DT[, grp := x%/% = 2]</div><div>DT[, lapply(.SD, sum), by=grp]</div><div><br></div><div>must be equal to:</div><div><br></div><div>DT[, lapply(.SD, sum), by = list(grp = x%/%2)] # here, "x" should be available to .SD as it's not the grouping column</div><div><br></div></div><div>Arun</div></div><div><div><br></div></div>