I have a data frame like so:
df<-data.frame(year= as.numeric(c(rep(1997, 5), rep(1998, 5), rep(1999, 5))),
sp= c("A", "B", "C", "D", "E", "A", "B", "C", "F", "G", "H", "I", "J","A", "B"))
I want to keep levels of sp
for which there are a minimum number of unique levels in year
. For this example, I want to keep sp
for which there are at least 2 years of data.
I have tried this:
df<-
df %>%
group_by(sp) %>%
filter(length(year) >= 2)
The correct output is:
output<- data.frame( year= c("1997", "1998", "1999","1997", "1998", "1999", "1997", "1998"),
sp= c("A", "A", "A", "B", "B", "B", "C", "C"))