【发布时间】:2022-08-11 02:54:10
【问题描述】:
我有一些看起来像这样的数据:
| id | ethnicity |
|---|---|
| 1 | white |
| 2 | south asian |
| 2 | other |
| 3 | other |
如上所示,如果一个 id 之前选择了其他种族值,那么他们有可能具有两个种族值。如果该 id 已经有诸如“white”或“south asian”之类的条目,我将如何删除这些“其他”行?
标签: r
我有一些看起来像这样的数据:
| id | ethnicity |
|---|---|
| 1 | white |
| 2 | south asian |
| 2 | other |
| 3 | other |
如上所示,如果一个 id 之前选择了其他种族值,那么他们有可能具有两个种族值。如果该 id 已经有诸如“white”或“south asian”之类的条目,我将如何删除这些“其他”行?
标签: r
我们可以用
library(dplyr)
df1 %>%
group_by(id) %>%
filter(!(any(ethnicity == 'other') &
any(ethnicity %in% c("white", "south asian"))) %>%
ungroup
【讨论】: