在R DataFrame中,识别/删除至少两个重复值的行

发布于 2025-02-02 19:22:59 字数 551 浏览 2 评论 0原文

假设我们有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谎言 2025-02-09 19:22:59

这是base r的一个选项 - 用应用在行上循环,检查重复项(anyduplicated - 返回第一个重复的索引,如果没有重复的,它返回0),然后否定( - 以便0变为true,而所有其他false)以子集-output子集

df[!apply(df, 1, anyDuplicated),]

-output

  x  y  z  w
1 10 30 40 50
3 30 40 10 50

Here is one option in base R - loop over the rows with apply, 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

df[!apply(df, 1, anyDuplicated),]

-output

  x  y  z  w
1 10 30 40 50
3 30 40 10 50
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文