data.table there a syntax in the j argument in data.table that allows me to refer to previously created variables in the same j expression? I am thinking of something like a Lisp let* construct.
library(data.table) set.seed(22) DT <- data.table(a = rep(1:5, each = 10), b = sample(c(0,1), 50, rep = TRUE)) DT[ , list(attempts = .N, successes = sum(b), rate = successes / attempts), by = a]
The result is
# Error in `[.data.table`(DT, , list(attempts = .N, successes = sum(b), :
I understand why, but is there any other way to accomplish this in the same j ?
source share