假设这是我的数据集。
我需要为两个组计算频率。
(dput)
dataset<-structure(list(group1 = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L), .Label = c("b", "x"), class = "factor"), group2 = structure(c(2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("g", "y"), class = "factor"),
var1 = c(2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L)), .Names = c("group1",
"group2", "var1"), class = "data.frame", row.names = c(NA, -9L
))
我需要为两个组计算频率。
x+y
b+g
对于变量var1,计算值为1和值为2的数量。对于每个组。
所需的输出结果为:
total_count_of_group var1-1 var1-2
x y 5
3 2
b g 4 2 2
这个输出的意思是,总共有x+y=5个观测值在此组中。其中1个值出现了3次,2个值出现了2次。
同样地, 总共有b+g=4个观测值在此组中。 其中1个值出现了2次,2个值出现了2次。
如何得到这样的表格?