Subset data frame to include only levels of one factor that have values in both levels of another factor
I am working with a data frame that deals with numeric measurements. Some individuals have been measured several times, both as juveniles and adults. A reproducible example: ID <- c("a1", "a2", "a3", "a4", "a1", "a2", "a5", "a6", "a1", "a3") age <- rep(c("juvenile", "adult"), each=5) size <- rnorm(10) # e.g. a1 is measured 3 times, twice as a juvenile, once as an adult. d <- data.frame(ID, age, size) My goal is to subset that data frame by selecting the IDs that appear at least once as a juvenile and at least once as an adult. Not sure how to do that..? The resulting dataframe would contain