我遵循了数据示例
df <- tibble(var1 = factor(c("a", "a", "a", "b", "b", "c", "c", "c")),
var2 = factor(c("a", "b", "b", "c", "c", "c", "d", "d")))
我想以tibble格式将这些数据总结为一行三列(1X3)。其中第一列显示相似行值的计数,第二列显示不同行值的计数,第三列显示最终格式为的值的总数:
final <- tibble(equal = 2, different = 6, total = 8)
谢谢大家
你可以用
library(dplyr)
df %>%
summarise(equal = sum(as.numeric(var1) == as.numeric(var2)),
different = sum(as.numeric(var1) != as.numeric(var2)),
total = n())
这返回
# A tibble: 1 x 3
equal different total
<int> <int> <int>
1 2 6 8