Can I use variables newly created in `j` in the same argument to` j`?

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), : # object 'successes' not found 

I understand why, but is there any other way to accomplish this in the same j ?

+6
source share
2 answers

This will do the trick:

 DT[ , { list(attempts = attempts <- .N, successes = successes <- sum(b), rate = successes/attempts) }, by = a] # a attempts successes rate # 1: 1 10 5 0.5 # 2: 2 10 6 0.6 # 3: 3 10 3 0.3 # 4: 4 10 5 0.5 # 5: 5 10 5 0.5 

FWIW, this closely related data.table function request will enable the +/- syntax used in your question. Quote from the linked page:

Summary:

Iterative RHS := (and `:=`(...) ) and several := inside j = {...} syntax

Detailed description

eg. DT[, `:=`( m1 = mean(a), m2 = sd(a), s = m1/m2 ), by = group]

where s can use the previous lhs names (using the word "iterative", tries to pass this).

+7
source

Try this instead:

 DT[, {successes = sum(b); attempts = .N; list(attempts = attempts, successes = successes, rate = successes / attempts) }, by = a] 

or

 DT[, list(attempts = .N, successes = sum(b)), by = a][, rate := successes / attempts] 
+4
source

Source: https://habr.com/ru/post/945180/


All Articles