【问题标题】:Using several fields on the same level in pivottabler在数据透视表中使用同一级别的多个字段
【发布时间】:2020-02-24 15:08:42
【问题描述】:

我正在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 个或更多字段并行而不是分层?

【问题讨论】:

  • 请使用该表重试。使用以三个 `(反引号)开头和结尾的代码块。在那里,将使用等距字体。另见:stackoverflow.com/help/formatting

标签: r pivot-table


【解决方案1】:

这可以通过在将人口组列添加到数据透视表时指定 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()

示例输出:

希望有帮助

克里斯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-06
    • 2021-01-01
    • 2019-02-16
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    相关资源
    最近更新 更多