我可以在同一个`j`参数中使用`j`中新创建的变量吗?

时间:2013-05-16 16:23:08

标签: r data.table

j中的data.table参数中,是否有语法允许我在同一j语句中引用先前创建的变量?我在考虑类似Lisp的let*构造。

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]

这导致

# Error in `[.data.table`(DT, , list(attempts = .N, successes = sum(b),  : 
#  object 'successes' not found

我理解为什么,但在同一j中有不同的方法来实现这一目标吗?

2 个答案:

答案 0 :(得分:7)

这样可以解决问题:

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 feature request可以使你的问题中使用+/-语法成为可能。从链接页面引用:

  

<强>要点:

     

:=(和`:=`(...))的迭代RHS,以及:=语法中的多个j = {...}

     

详细说明

     

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

     

其中s可以使用先前的lhs名称(使用'iterative'一词尝试传达它)。

答案 1 :(得分:4)

请改为尝试:

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

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