首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在pivottabler中使用同一级别上的多个字段

在pivottabler中使用同一级别上的多个字段
EN

Stack Overflow用户
提问于 2020-02-24 23:08:42
回答 1查看 52关注 0票数 2

我在pivottabler中寻找为2个并行字段构建表的选项,如下所示:

代码语言:javascript
复制
             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或更多字段中并行添加,而不是分层添加?

EN

回答 1

Stack Overflow用户

发布于 2020-04-13 01:00:08

这可以通过在向数据透视表中添加填充组列时指定atLevel=1来完成。

我在下面的示例中编写了一些示例数据。

代码语言:javascript
复制
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()

输出示例:

希望这能有所帮助

克里斯

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60378719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档