【发布时间】:2017-10-11 20:10:09
【问题描述】:
这是一个虚拟数据:
father<- c(1, 1, 1, 1, 1)
mother<- c(1, 1, 1, NA, NA)
children <- c(NA, NA, 2, 5, 2)
cousins <- c(NA, 5, 1, 1, 4)
dataset <- data.frame(father, mother, children, cousins)
dataset
father mother children cousins
1 1 NA NA
1 1 NA 5
1 1 2 1
1 NA 5 1
1 NA 2 4
我想过滤这一行:
father mother children cousins
1 1 NA NA
我可以做到:
test <- dataset %>%
filter(father==1 & mother==1) %>%
filter (is.na(children)) %>%
filter (is.na(cousins))
test
我的问题: 我有很多列,例如祖父、叔叔1、叔叔2、叔叔3,我想避免这样的事情:
filter (is.na(children)) %>%
filter (is.na(cousins)) %>%
filter (is.na(uncle1)) %>%
filter (is.na(uncle2)) %>%
filter (is.na(uncle3))
and so on...
我如何使用 dplyr 过滤所有带有 na 的列(父亲==1 和母亲==1 除外)
【问题讨论】:
-
IMO 您应该将数据转换为长格式 (stackoverflow.com/questions/2185252/…)