如果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
包中的解决方案。
答案 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]])