我在pivottabler中寻找为2个并行字段构建表的选项,如下所示:
SEX | POPULATION GROUPS
______________________|_______________________________________
1 | 2 | 1 | 2 | 3 | 4
___________|_________|_________|_________|_________|___________
AgeGroups |AgeGroups|AgeGroups|AgeGroups|AgeGroups|AgeGroups
| | | | | | | | | | | | | | | | |
1 | 2| 3| 1 | 2| 3| 1 | 2| 3| 1 |2 | 3| 1 | 2| 3| 1 | 2| 3
______|__|__|___|__|__|___|__|__|___|__|__|___|__|__|___|__|___
| | | | | | | | | | | | | | | | | 如何在AddColumnDataRows 2或更多字段中并行添加,而不是分层添加?
发布于 2020-04-13 01:00:08
这可以通过在向数据透视表中添加填充组列时指定atLevel=1来完成。
我在下面的示例中编写了一些示例数据。
n <- 100
sex <- sample(x=c("M","F"), size=n, replace=TRUE)
pg <- sample(x=c("pg1","pg2","pg3","pg4"), size=n, replace=TRUE)
ag <- sample(x=c("ag1","ag2","ag3"), size=n, replace=TRUE)
grp <- sample(x=c("g1","g2","g3","g4"), size=n, replace=TRUE)
df <- data.frame(sex, pg, ag, grp)
library(pivottabler)
pt <- PivotTable$new()
pt$addData(df)
pt$addColumnDataGroups("sex", addTotal=FALSE)
pt$addColumnDataGroups("pg", atLevel=1, addTotal=FALSE)
pt$addColumnDataGroups("ag", addTotal=FALSE)
pt$addRowDataGroups("grp")
pt$defineCalculation(calculationName="Count", summariseExpression="n()")
pt$renderPivot()输出示例:

希望这能有所帮助
克里斯
https://stackoverflow.com/questions/60378719
复制相似问题