I have a sample data set as below:
df <- data.frame(Group = c("a", "d", "a", "b", "b", "c", "c", "c", "c"),
Year = c("1991", '1992', '1993', '1991', '1992', '1991', '1992','1993','1994'),
value = 1:9)
I want to select rows that have the same group as 1991. For example, the groups are a, b, c in 1991 and d, b, c in 1992; therefore, group b, and c are selected for 1992. The groups are a and c in 1993; therefore, only a and c are selected in 1993. The resulting output is this
Group Year
a 1991
b 1991
c 1991
b 1992
c 1992
a 1993
c 1993
This is what I tried:
df2 <- df %>% group_by(Group, Year) %>%
mutate(total = n()) %>%
filter(total == 3)
I can change total == 3
to total==2
, but regardless, it filters observations so that each year have the same group, while I want to base the selection criteria only on 1991.