Setting column name in “group by” operation with data.table

前端 未结 2 1716
天命终不由人
天命终不由人 2020-12-31 05:02

I am a new user of the data.table package in R. I am trying to give a name to the new column created by a \"group by\" command

> DT = data.table(x=rep(c(\         


        
相关标签:
2条回答
  • 2020-12-31 05:07
    DT[,list(z=sum(y)+3,a=mean(y*z)),by=x]
       x  z  a
    1: a  6  9
    2: b 15 60
    

    Since you are new to data.table, I recommend that you also study the help page of the setnames function as well as ?data.table and the data.table vignettes.

    0 讨论(0)
  • 2020-12-31 05:10

    For conciseness, you can now use .() instead of list()

    DT[, .(z=sum(y)+3, a=mean(y*z)), by=x]
    
    0 讨论(0)
提交回复
热议问题