我有这个data.frame:

df <- data.frame(
    id = c("x1", "x2", "x3", "x4", "x5", "x1", "x2", "x6", "x7", "x8", "x7", "x8" ),
    age = c(rep("juvenile", 5), rep("adult", 7))
    )
df

   id      age
1  x1 juvenile
2  x2 juvenile
3  x3 juvenile
4  x4 juvenile
5  x5 juvenile
6  x1    adult
7  x2    adult
8  x6    adult
9  x7    adult
10 x8    adult
11 x7    adult
12 x8    adult

每行代表一个人。我想撤出所有将青少年重新视为成年人的行。我不希望将原本是成年人的人再次视为成年人的行(id x7和x8)。因此,结果data.frame应该是这样的:
  id      age
1 x1 juvenile
2 x2 juvenile
3 x1    adult
4 x2    adult

我特别喜欢dplyr解决方案。

最佳答案

您可以按id分组,并仅选择同时包含“少年”和“成人”的那些组:

df %>%
   group_by(id) %>%
   filter(all(c('juvenile','adult') %in% age))

#Source: local data frame [4 x 2]
#Groups: id
#
#  id      age
#1 x1 juvenile
#2 x2 juvenile
#3 x1    adult
#4 x2    adult

10-05 19:30