ein Beispiel unter Verwendung data.table
(und unique
mehr Reihen von derselben Person in demselben Jahr zu handhaben):
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
Die erwartete Ausgabe ist nicht klar. – Misaki
habe ich gerade den Datenrahmen korrigiert. Ich möchte Personen, die in 2015 und 2016 sind. PersonB ist in beiden Datenrahmen. – rstudy12
Sie sind in der Nähe. Dieses Thema wird ausführlich in SO behandelt und Sie finden ein relevantes Beispiel (zB [hier] (http://stackoverflow.com/questions/4935479/how-to-combine-multiple-conditions-to-subset-a-data- frame-using-or)) – metasequoia