使用两列过滤条件

时间:2018-01-24 14:12:10

标签: r filter dplyr

如果NA = value,我想删除crime列中"total"个值的城市。所以我不只是想删除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包中的解决方案。

1 个答案:

答案 0 :(得分:0)

更容易阅读的块:

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]])