【发布时间】: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' & is.na(df$value)],] -
我们不使用标题上的语言。这就是为什么我们有标签