Assume I have the following data frame:
df <- data.frame(year=c(2010,2011,2012,2010,2011,2010,2011,2012), company = c("a","a","a","b","b","c","c","c"))
year company
1 2010 a
2 2011 a
3 2012 a
4 2010 b
5 2011 b
6 2010 c
7 2011 c
8 2012 c
I want to find the companies that are present in all three years. One cumbersome approach would be:
library(dplyr)
companies_2010 <- df %>% filter(year==2010) %>% select(company)
companies_2011 <- df %>% filter(year==2011) %>% select(company)
companies_2012 <- df %>% filter(year==2012) %>% select(company)
companies <- intersect(companies_2010, companies_2011) %>% intersect(., companies_2012)
company
1 a
2 c
Is there any more elegant way to do this?

