虚拟数据框:
id_family<- c(1, 1, 2, 2, 3, 3)
people<- c("male", "female", "male", "female", "male", "children")
dataset <- data.frame(id_family, people)
dataset
我的结果:
id_family people
1 male
1 female
2 male
2 female
3 male
3 children
我想要的是:根据“男性和女性”序列过滤行
预期结果:过滤家庭1和2
id_family people
1 male
1 female
2 male
2 female
我尝试使用lag / lead dplyr的功能但没有成功:
dataset2 <- dataset %>%
filter(people=="male", lead(people)=="female")
答案 0 :(得分:2)
我们可以使用all
dataset %>%
group_by(id_family) %>%
filter(all(c("male", "female") %in% people))
# A tibble: 4 x 2
# Groups: id_family [2]
# id_family people
# <dbl> <fctr>
#1 1 male
#2 1 female
#3 2 male
#4 2 female
或者根据OP的评论,如果订单很重要,那么
dataset %>%
group_by(id_family) %>%
filter(first(people)=="male", last(people) == "female", n()==2)