【问题标题】:Filter on conditionals with two columns用两列过滤条件
【发布时间】:2018-01-24 23:03:36
【问题描述】:

如果crime = "total",我想删除value 列中具有NA 值的城市。所以我不只是想删除具有NA 值的行,而是想删除该城市的所有行。

这是一个示例数据框:

df <- structure(list(city = c("Amsterdam", "Amsterdam", "Amsterdam", 
"Rotterdam", "Rotterdam", "Rotterdam"), year = c(2015L, 2016L, 
2017L, 2015L, 2016L, 2017L), crime = c("total", "total", "total", 
"total", "total", "total"), value = c(5000L, 5190L, NA, 4901L, 
4830L, 4659L)), .Names = c("city", "year", "crime", "value"), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"), spec = structure(list(
    cols = structure(list(city = structure(list(), class = c("collector_character", 
    "collector")), year = structure(list(), class = c("collector_integer", 
    "collector")), crime = structure(list(), class = c("collector_character", 
    "collector")), value = structure(list(), class = c("collector_integer", 
    "collector"))), .Names = c("city", "year", "crime", "value"
    )), default = structure(list(), class = c("collector_guess", 
    "collector"))), .Names = c("cols", "default"), class = "col_spec"))

我更喜欢dplyr 包中的解决方案。

【问题讨论】:

  • df[!df$city %in% df$city[df$crime == 'total' &amp; is.na(df$value)],]
  • 我们不使用标题上的语言。这就是为什么我们有标签

标签: r filter dplyr


【解决方案1】:

更容易阅读的块:

city_List <- df%>%filter(is.na(value) & crime == "total")%>%distinct(city)
df%>%filter(!city %in% city_List)

单线:

df%>%filter(!city %in% list(filter(df,is.na(value) & crime == "total")%>%distinct(city))[[1]])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    相关资源
    最近更新 更多