rpivotTable
由于package in ,我有一个使用此代码生成的 rpivottable R
:
library("rpivotTable")
library("dplyr")
library("reshape2")
dane <- melt(HairEyeColor)
rpivotTable(dane,
rows = c("Hair", "Eye"),
cols = c("Sex"),
vals = "value",
aggregatorName = "Integer Sum",
locale = "en",
rendererName = "Table With Subtotal",
subtotals = TRUE)
看起来像这样:
它按字母顺序排序。我想使用值的总和按降序对其进行排序。
我可以这样尝试:
library("rpivotTable")
library("dplyr")
library("reshape2")
dane <- melt(HairEyeColor)
sorter <- paste0("function(attr) {",
"var sortAs = $.pivotUtilities.sortAs;",
"if (attr == \"Eye\") { return sortAs([\"",
dane %>% group_by(Eye) %>% summarise(i = sum(value)) %>% arrange(-i) %>% .$Eye %>% paste(collapse = "\", \""),
"\"]); }",
"if (attr == \"Hair\") { return sortAs([\"",
dane %>% group_by(Hair) %>% summarise(i = sum(value)) %>% arrange(-i) %>% .$Hair %>% paste(collapse = "\", \""),
"\"]); }",
"}")
rpivotTable(dane,
rows = c("Hair", "Eye"),
cols = c("Sex"),
vals = "value",
aggregatorName = "Integer Sum",
locale = "en",
rendererName = "Table With Subtotal",
subtotals = TRUE,
sorters = sorter)
比我得到这个:
这是按“外部”组排序的。我想让它按两个组排序,如下所示:
可以rpivotTable
打包吗?