我有一个 df 数据框,其中包含以下列:
df <- data.frame(
years = c(2010, 2010, 2011, 2011),
category = c("Category1", "Category2", "Category1", "Category2"),
type = c("Type1", "Type2", "Type1", "Type2"),
theme = c("Theme1", "Theme2", "Theme1", "Theme2"),
count = c(40, 50, 10, 70)
)
我想在 R 中制作一个这样的表格:
Category1 Category2
2010 | 2011 2010 | 2011
Type1
Theme1 x x x x
Type2
Theme2 x x x x
其中 x 是每种类型和每个主题每年每个类别的平均计数。
关于如何做到这一点有什么想法吗?
这不会完全相同,但您可以使用 table1 包来获得与您正在寻找的类似的东西。
library(table1)
table1(~ count + type + theme | category * years, data = df, topclass = "Rtable1-grid Rtable1-shade Rtable1-times", overall = FALSE, render.continuous = "Mean")