Aggregating sub totals and grand totals with data.table

前端 未结 5 2533
一向
一向 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-14 04:23

    See below for a solution - similar to @MattDowle's above - that takes any number of groups.

    crossby2 <- function(data, j, by, grand.total = T, total.label = "(all)", value.label = "value") {
      j = substitute(j)
    
      # Calculate by each group
      lst <- lapply(1:length(by), function(i) {
        x <- data[, list(..VALUE.. = eval(j)), by = eval(by[1:i])]
        if (i != length(by)) x[, (by[-(1:i)]) := total.label]
        return(x)
      })
    
      # Grand total
      if (grand.total) lst <- c(lst, list(data[, list(..VALUE.. = eval(j))][, (by) := total.label]))
    
      # Combine all tables
      res <- rbindlist(lst, use.names = T, fill = F)
    
      # Change value column name
      setnames(res, "..VALUE..", value.label)
    
      # Set proper column order
      setcolorder(res, c(by, value.label))
    
      # Sort values
      setkeyv(res, by)
    
      return(res)
    }
    

提交回复
热议问题