shenwei356 / csvtk

A cross-platform, efficient and practical CSV/TSV toolkit in Golang
http://bioinf.shenwei.me/csvtk
MIT License
992 stars 84 forks source link

rollups #274

Closed ggrothendieck closed 3 months ago

ggrothendieck commented 4 months ago

Suggest supporting rollups. For example, here is a rollup using R.

# input data
dat <- data.frame(
   year = rep(2000:2001, each = 5L),
   month = rep(rep(1:2, 2), rep(2:3, 2)),
   value = c(34L, 42L, 18L, 22L, 6L, 3L, 2L, 8L, 2L, 9L),
   n = 1:10
)
dat
##    year month value  n
## 1  2000     1    34  1
## 2  2000     1    42  2
## 3  2000     2    18  3
## 4  2000     2    22  4
## 5  2000     2     6  5
## 6  2001     1     3  6
## 7  2001     1     2  7
## 8  2001     2     8  8
## 9  2001     2     2  9
## 10 2001     2     9 10

# compute rollup
library(reshape2)
m <- melt(dat, measure.vars = "value")
dcast(m, year + month + n ~ variable, fun.aggregate = sum, margins = TRUE)
##     year month     n value (all)
## 1   2000     1     1    34    34
## 2   2000     1     2    42    42
## 3   2000     1 (all)    76    76
## 4   2000     2     3    18    18
## 5   2000     2     4    22    22
## 6   2000     2     5     6     6
## 7   2000     2 (all)    46    46
## 8   2000 (all) (all)   122   122
## 9   2001     1     6     3     3
## 10  2001     1     7     2     2
## 11  2001     1 (all)     5     5
## 12  2001     2     8     8     8
## 13  2001     2     9     2     2
## 14  2001     2    10     9     9
## 15  2001     2 (all)    19    19
## 16  2001 (all) (all)    24    24
## 17 (all) (all) (all)   146   146
shenwei356 commented 3 months ago

Sorry, no plan for this.