在R DataFrame中,识别/删除至少两个重复值的行
假设我们有一个R DataFrame。我们如何识别(和删除)至少两次值的任何行?搜索后,我仍然找不到网络上的解决方案。一个小的代码示例说明了我的意思:
> df <- data.frame(x = c(10, 20, 30, 50), y = c(30, 40, 40, 50),
z = c(40, 50, 10, 50), w = c(50, 40, 50, 50))
这给出了数据框
>df
x y z w
1 10 30 40 50
2 20 40 50 40
3 30 40 10 50
4 50 50 50 50
,因此,DF在第2和4行中具有重复的值,我想删除这些行,以获取result
:
> result
x y z w
1 10 30 40 50
3 30 40 10 50
对于我的应用程序i可以使用一个假设只有四列的解决方案,尽管当然,一般解决方案会更好。
Suppose we have an R dataframe. How can we identify (and remove) any rows where some value occurs at least two times? After some searching, I still can not find a solution on the web. A small code example illustrates what I am after:
> df <- data.frame(x = c(10, 20, 30, 50), y = c(30, 40, 40, 50),
z = c(40, 50, 10, 50), w = c(50, 40, 50, 50))
This gives the dataframe
>df
x y z w
1 10 30 40 50
2 20 40 50 40
3 30 40 10 50
4 50 50 50 50
So, df has duplicate values in row 2 and 4, and I want to remove those rows, to get the result
:
> result
x y z w
1 10 30 40 50
3 30 40 10 50
For my application I can use a solution where one assumes there are just four columns, although of course a general solution would be better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是
base r
的一个选项 - 用应用
在行上循环,检查重复项(anyduplicated
- 返回第一个重复的索引,如果没有重复的,它返回0),然后否定(!
- 以便0变为true,而所有其他false)以子集-output子集-output
Here is one option in
base R
- loop over the rows withapply
, check for duplicates (anyDuplicated
- return the index of first duplicate, if no duplicates, it returns 0), then negate (!
- so that 0 becomes TRUE and all others FALSE) to subset the rows-output