我最初有一个不同的数据(4 行,158 列),我用reshape::melt()
它来创建一个长数据集(624 行 x 3 列)。
但是,现在我有一个这样的数据集:
demo <- data.frame(region = as.factor(c("North", "South", "East", "West")),
criteria = as.factor(c("Writing_1_a", "Writing_2_a", "Writing_3_a", "Writing_4_a",
"Writing_1_b", "Writing_2_b", "Writing_3_b", "Writing_4_b")),
counts = as.integer(c(18, 27, 99, 42, 36, 144, 99, 9)))
这会产生一个类似于下面的表格:
region criteria counts
North Writing_1_a 18
South Writing_2_a 27
East Writing_3_a 99
West Writing_4_a 42
North Writing_1_b 36
South Writing_2_b 144
East Writing_3_b 99
West Writing_4_b 9
现在我想要创建的是这样的:
goal <- data.frame(region = as.factor(c("North", "South", "East", "West")),
criteria = as.factor(c("Writing_1", "Writing_2", "Writing_3", "Writing_4")),
counts = as.integer(c(54, 171, 198, 51)))
这意味着当我折叠标准列时,它会汇总计数:
region criteria counts
North Writing_1 54
South Writing_2 171
East Writing_3 198
West Writing_4 51
我试过使用forcats::fct_collapse
但forcats::recode()
无济于事 - 我很肯定我只是做得不对。提前感谢您提供的任何帮助。