如何计算出现在两列中的频率?样本数据:
> sample <- dput(df)
structure(list(Nom_xp = c("A1FAA", "A1FAJ", "A1FBB", "A1FJA",
"A1FJR", "A1FRJ"), GB05.x = c(100L, 98L, NA, 100L, 102L, 98L),
GB05.1.x = c(100L, 106L, NA, 100L, 102L, 98L), GB18.x = c(175L,
173L, 177L, 177L, 173L, 177L), GB18.1.x = c(177L, 175L, 177L,
177L, 177L, 177L)), .Names = c("Nom_xp", "GB05.x", "GB05.1.x",
"GB18.x", "GB18.1.x"), row.names = c(NA, 6L), class = "data.frame")
计数频率:
apply(sample[,2:5],2,table)
现在,如何按列的前缀或每两列组合计数?前四列的预期输出将是一个列表:
$GB05
98 100 102 106
3 4 2 1
$GB18
173 175 177
2 2 8
获取前两列计数的一种方法:
table(c(apply(sample[,2:3],2,rbind)))
98 100 102 106
3 4 2 1
但是如何将其应用于整个 data.frame ?