An example using data.table
(and unique
to handle multiple rows of same person in same year):
library(data.table)
dt <- structure(list(persons = c("personA", "personB", "personC", "personB"
), year = c(2015L, 2016L, 2015L, 2015L)), .Names = c("persons",
"year"), row.names = c(NA, -4L), class = "data.frame")
setDT(dt)
years <- c("2015", "2016")
# Filter by years and make sure all rows are unique combinations of persons and
# thoese years. Then set in_all_years to TRUE of number of rows is equal to
# number of years
out <- unique(dt[year %in% years])[, in_all_years := .N == length(years),
by = persons]
> out
persons year in_all_years
1: personA 2015 FALSE
2: personB 2016 TRUE
3: personC 2015 FALSE
4: personB 2015 TRUE