Aggregating sub totals and grand totals with data.table

前端 未结 5 2531
一向
一向 2020-12-14 03:41

I\'ve got a data.table in R:

library(data.table)
set.seed(1)
DT = data.table(
  group=sample(letters[1:2],100,replace=TRUE), 
  year=sample(2010         


        
5条回答
  •  鱼传尺愫
    2020-12-14 04:28

    In recent devel data.table you can use new feature called "grouping sets" to produce sub totals:

    library(data.table)
    set.seed(1)
    DT = data.table(
        group=sample(letters[1:2],100,replace=TRUE), 
        year=sample(2010:2012,100,replace=TRUE),
        v=runif(100))
    
    cube(DT, mean(v), by=c("group","year"))
    #    group year        V1
    # 1:     a 2011 0.4176346
    # 2:     b 2010 0.5231845
    # 3:     b 2012 0.4306871
    # 4:     b 2011 0.4997119
    # 5:     a 2012 0.4227796
    # 6:     a 2010 0.2926945
    # 7:    NA 2011 0.4463616
    # 8:    NA 2010 0.4278093
    # 9:    NA 2012 0.4271160
    #10:     a   NA 0.3901875
    #11:     b   NA 0.4835788
    #12:    NA   NA 0.4350153
    cube(DT, mean(v), by=c("group","year"), id=TRUE)
    #    grouping group year        V1
    # 1:        0     a 2011 0.4176346
    # 2:        0     b 2010 0.5231845
    # 3:        0     b 2012 0.4306871
    # 4:        0     b 2011 0.4997119
    # 5:        0     a 2012 0.4227796
    # 6:        0     a 2010 0.2926945
    # 7:        2    NA 2011 0.4463616
    # 8:        2    NA 2010 0.4278093
    # 9:        2    NA 2012 0.4271160
    #10:        1     a   NA 0.3901875
    #11:        1     b   NA 0.4835788
    #12:        3    NA   NA 0.4350153
    

提交回复
热议问题