【问题标题】:How to a row in a dataframe based on certain conditions如何根据特定条件在数据框中排列一行
【发布时间】:2022-08-11 02:54:10
【问题描述】:

我有一些看起来像这样的数据:

id ethnicity
1 white
2 south asian
2 other
3 other

如上所示,如果一个 id 之前选择了其他种族值,那么他们有可能具有两个种族值。如果该 id 已经有诸如“white”或“south asian”之类的条目,我将如何删除这些“其他”行?

    标签: r


    【解决方案1】:

    我们可以用

    library(dplyr)
    df1 %>%
       group_by(id) %>%
       filter(!(any(ethnicity == 'other') &
               any(ethnicity %in% c("white", "south asian"))) %>%
       ungroup
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 2021-12-19
      • 2020-02-24
      • 2016-08-06
      • 1970-01-01
      • 2020-01-10
      • 1970-01-01
      相关资源
      最近更新 更多