If I group using the by keyword in data.table, it always returns the by column as the first column. Is there a flag / option to tell this not to be done? Or a smart way to get rid of it?
In particular, I want to group and then rbindlist into my original table, so in fact the problem can also be called as "how to stop its reordering columns"
For example:
DT = data.table(I = as.numeric(1:6), N = rnorm(6), L = rep(c("a", "b", "c"), 2)) DT[, list(I = mean(I), N = mean(N)), by= L] DT
gives:
> DT[, list(I = mean(I), N = mean(N)), by= L] LIN 1: a 2.5 0.4291802 2: b 3.5 0.6669517 3: c 4.5 -0.6471886 > DT INL 1: 1 1.8460998 a 2: 2 0.7093438 b 3: 3 -1.7991193 c 4: 4 -0.9877394 a 5: 5 0.6245596 b 6: 6 0.5047421 c
Regarding the rbindlist request, it would be nice to be able to do this:
DT = rbindlist(list(DT, DT[, list(I = mean(I), N = mean(N)), by= L]))
or maybe
DT = rbindlist(list(DT, DT[, list(I = mean(I), N = mean(N), L), by= L]))
or something similar (none of them work)
Corone
source share