根据另一列中的值在一个列中重复复制的行删除行

发布于 2025-02-09 08:25:04 字数 626 浏览 3 评论 0原文

提出了一个类似的问题在这里。但是,我没有设法对我的特定问题采用该解决方案,因此是一个单独的问题。

一个示例数据集:


  id group
1  1   5
2  1 998
3  2   2
4  2   3
5  3 998

我想删除在ID中重复的所有行且组具有值998的行。 在此示例中,仅应删除第2行。

我尝试了一些事情:

df1 <- df %>%
  subset((unique(by = "id") |  group != 998))

但是

ins.factor(x)中的错误(X):参数“ x”,没有默认值

默认

A similar question was asked here. However, I did not manage to adopt that solution to my particular problem, hence the separate question.

An example dataset:


  id group
1  1   5
2  1 998
3  2   2
4  2   3
5  3 998

I would like to delete all rows that are duplicated in id and where group has value 998.
In this example, only row 2 should be deleted.

I tried something along those lines:

df1 <- df %>%
  subset((unique(by = "id") |  group != 998))

but got

Error in is.factor(x) : Argument "x" is missing, with no default

Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

北方。的韩爷 2025-02-16 08:25:04

这是一个想法,

library(dplyr)

df %>% 
 group_by(id) %>% 
 filter(!any(n() > 1 & group == 998))

# A tibble: 3 x 2
# Groups:   id [2]
     id group
  <int> <int>
1     2     2
2     2     3
3     3   998

如果您只想从组中删除998条目,

df %>% 
 group_by(id) %>% 
 filter(!(n() > 1 & group == 998))

Here is an idea

library(dplyr)

df %>% 
 group_by(id) %>% 
 filter(!any(n() > 1 & group == 998))

# A tibble: 3 x 2
# Groups:   id [2]
     id group
  <int> <int>
1     2     2
2     2     3
3     3   998

In case you want to remove only the 998 entry from the group then,

df %>% 
 group_by(id) %>% 
 filter(!(n() > 1 & group == 998))
花落人断肠 2025-02-16 08:25:04

一种方法可能是:

library(dplyr)

df1 <- df %>% 
  filter(duplicated(id) & group=="998") 

anti_join(df, df1)
Joining, by = c("id", "group")
  id group
1  1     5
3  2     2
4  2     3
5  3   998

One way could be:

library(dplyr)

df1 <- df %>% 
  filter(duplicated(id) & group=="998") 

anti_join(df, df1)
Joining, by = c("id", "group")
  id group
1  1     5
3  2     2
4  2     3
5  3   998
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文