这是另一种仅使用基数 R 并且不对一个人每年拥有的记录数量做出任何假设的解决方案:
d <- data.frame(cbind(numbers = rnorm(10),
year = rep(c(2008, 2009), 5),
name = c("john", "David", "Tom", "Kristin",
"Lisa","Eve","David","Tom","Kristin",
"Lisa")))
# split data into 2 data.frames (1 for each year)
by.year <- split(d, d$year, drop=T)
# find the names that appear in both years
keep <- intersect(by.year[['2008']]$name, by.year[['2009']]$name)
# Or, if you had several years, use Reduce as a more general solution:
keep <- Reduce(intersect, lapply(by.year, '[[', 'name'))
# show the rows of the original dataset only if their $name field
# is in our 'keep' vector
d[d$name %in% keep,]