查找重复行在 R 数据框中重复的次数 [英] Find how many times duplicated rows repeat in R data frame

查看:58
本文介绍了查找重复行在 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

这篇关于查找重复行在 R 数据框中重复的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆