R - Pivot table with subtotals

﹥>﹥吖頭↗ 提交于 2019-12-06 13:29:19
A5C1D2H2I1M1N2O1R2T1

Assuming you're starting without any totals, with something like this:

mydf <- structure(list(C1 = c("a", "a", "b", "b", "b"), X1 = c(12L, 14L, 
    16L, 11L, 8L), X2 = 1:5), .Names = c("C1", "X1", "X2"), row.names = c(NA, 
    5L), class = "data.frame")

mydf
##   C1 X1 X2
## 1  a 12  1
## 2  a 14  2
## 3  b 16  3
## 4  b 11  4
## 5  b  8  5

Then you would have to use the margins argument for dcast to get the output you're after.

library(reshape2)
mydfl <- melt(mydf)
mydfl$ind <- with(mydfl, ave(C1, C1, variable, FUN = seq_along))
dcast(mydfl, C1 + ind ~ variable, sum, 
      margins = c("C1", "ind", "variable"))
#      C1   ind X1 X2 (all)
# 1     a     1 12  1    13
# 2     a     2 14  2    16
# 3     a (all) 26  3    29
# 4     b     1 16  3    19
# 5     b     2 11  4    15
# 6     b     3  8  5    13
# 7     b (all) 35 12    47
# 8 (all) (all) 61 15    76

The "margins" argument is what is used to get the subtotals. Here, we're taking subtotals according to the "C1" (Grand Total) and "ind" variable (Subtotal), and we're also adding row totals (using "variable").

That said, I'm not sure if I'd recommend this as you're sort of mixing data and analysis in the same table.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!