Reference to the previous variable in the same chain in data.table

In the following example, how do I refer to a variable (s) in the same chain, but only get evaluated earlier:

library(data.table)
mtdt<-as.data.table(mtcars)
mtdt[,.(mpg1=mean(mpg),disp1=mean(disp)),by=cyl][,`:=`(mbyd=mpg1/disp1)] # this works

      

But I was looking for a solution like this:

mtdt[,.(mpg1=mean(mpg),disp1=mean(disp),mbyd=mpg1/disp1),by=cyl] 
Error in `[.data.table`(mtdt, , .(mpg1 = mean(mpg), disp1 = mean(disp),  : 
  object 'mpg1' not found

      

I've always thought it was okay to reference the previous variable (s). Maybe I missed something?

PS I am NOT looking for a solution from other packages.

+3


source to share


1 answer


If we need to create three new variables in an existing dataset,

mtdt[, c('mpg1', 'disp1', 'mybd') := {tmp1 <- mean(mpg)
                                      tmp2 <- mean(disp)
                                     list(tmp1, tmp2, tmp1/tmp2)}, by = cyl]

      



Or, if we are only interested in a summary of three variables along with 'cyl'

mtdt[, {tmp1 <- mean(mpg)
        tmp2 <- mean(disp)
        list(mpg1 = tmp1, disp1 = tmp2, mybd = tmp1/tmp2)}, by = cyl]
#   cyl     mpg1    disp1       mybd
#1:   6 19.74286 183.3143 0.10769950
#2:   4 26.66364 105.1364 0.25361003
#3:   8 15.10000 353.1000 0.04276409

      

+4


source







All Articles