计算R中数据帧中每个唯一行的数量

时间:2018-11-16 20:21:44

标签: r

这是asked on CrossValidated,但答案似乎与R的最新版本不兼容。

df<-data.frame(x1=c(0,1,1,1,2,3,3,3),
x2=c(0,1,1,3,2,3,3,2),
x3=c(0,1,1,1,2,3,3,2))
df
  x1 x2 x3
1  0  0  0
2  1  1  1
3  1  1  1
4  1  3  1
5  2  2  2
6  3  3  3
7  3  3  3
8  3  2  2

我要计算的是每个唯一行的数量,例如:

  x1 x2 x3 count
1  0  0  0  1
2  1  1  1  2
4  1  3  1  1
5  2  2  2  1 
6  3  3  3  2
8  3  2  2  1

在R中实现它的最简单方法是什么?

答案是:

library(plyr)
df = data.frame(x1=c(0,1,1,1,2,3,3,3),
               x2=c(0,1,1,3,2,3,3,2),
               x3=c(0,1,1,1,2,3,3,2))

count(df, vars = c("x1", "x2", "x3"))

但是当我使用它时,我得到:

Error in mutate_impl(.data, dots) : 
  Column `vars` must be length 8 (the number of rows) or one, not 3

如何获取每一行出现在数据框中的次数?

0 个答案:

没有答案
相关问题