Background
The main property data.tableis that
"As long as it jreturns a list, each list item becomes a column in the resulting data table."
This is shown, for example. in the example from ?data.table:
library(data.table)
DT[, c(.N, lapply(.SD, sum)), by=x]
Here the integer is .Nequal cto which is included in the list obtained as a result lapply, and the total result is a list, i.e. .Nimplicitly coerced to a list item (according to the coercion hierarchy described in ?c)
What caught my attention was the example used both in ?data.tableand in .SD, where, unlike the above, the non-list part is jexplicitly converted to a list:
DT[, c(.(y=max(y)), lapply(.SD, min)), by=rleid(v), .SDcols=v:b]
It does not immediately seem to me why the single number obtained in y=max(y)this example is converted to list ( .()) when it is converted to a list item in any case, after concatenation with list ( lapply(.SD.,).
Explicit is listnot needed?
Here is a small example, where max(y)and sumall the variables are calculated using the variable group. structure and the result of calculations really coincide, both when explicitly converting the result of "non-list" to ja list, and in the absence of this:
dt <- data.table(grp = rep(c("a", "b"), 2:3), x = 1:5, y = 2:6)
# - structure of j
dt[ , str(c(.(ymax = max(y)), lapply(.SD, sum))), by = grp]
dt[ , str(c(ymax = max(y), lapply(.SD, sum))), by = grp]
# - result of j
dt[ , c(.(ymax = max(y)), lapply(.SD, sum)), by = grp]
dt[ , c(ymax = max(y), lapply(.SD, sum)), by = grp]
# ...both give the same result:
, j . ? , .N .
list, .N!
"non-list" j .N, j , .
str j , .N, j:
dt[ , str(c(.(n = .N), lapply(.SD, sum))), by = grp]
dt[ , str(c(n = .N, lapply(.SD, sum))), by = grp]
~~ > , str ".N" , j, "n".
, .N "list ed" j, .N "N" (. ?.N) :
dt[ , c(.(n = .N), lapply(.SD, sum)), by = grp]
dt[ , c(n = .N, lapply(.SD, sum)), by = grp]
, .(), , , j / .
.N ( ) , list, , ? , ?