data.table drop key rows and summarize

后端 未结 1 1638
天命终不由人
天命终不由人 2021-01-19 00:40

I\'m looking for an elegant way to iterate over the key of data.table, drop the rows that have that key, then take a summary over the remaining rows. For example:

         


        
相关标签:
1条回答
  • I think this does it, using more traditional data.table code:

    setkey(mydt,cat)
    mydt[, list(means=mean(mydt[!.BY,vals])), by=cat]
    
    # or without needing to key first
    mydt[, list(means=mean(mydt[cat != .BY,vals])), by=cat]
    
    #   cat means
    #1:   a   5.0
    #2:   b   4.2
    #3:   c   2.5
    
    0 讨论(0)
提交回复
热议问题