查找重复行在 R 数据框中重复的次数
问题描述:
我有一个如下例所示的数据框
I have a data frame like the following example
a = c(1, 1, 1, 2, 2, 3, 4, 4)
b = c(3.5, 3.5, 2.5, 2, 2, 1, 2.2, 7)
df <-data.frame(a,b)
我可以通过以下代码从 R 数据框中删除重复的行,但是如何找到每个重复行重复的次数?我需要结果作为向量.
I can remove duplicated rows from R data frame by the following code, but how can I find how many times each duplicated rows repeated? I need the result as a vector.
unique(df)
或
df[!duplicated(df), ]
答
这里是使用库 plyr
library(plyr)
ddply(df,.(a,b),nrow)
a b V1
1 1 2.5 1
2 1 3.5 2
3 2 2.0 2
4 3 1.0 1
5 4 2.2 1
6 4 7.0 1