只需将tapply
摘要与原始数据框“合并”回merge
.
由于您没有提供示例数据,因此我做了一些。相应地修改。
n = 1000
id = sample(1:10, n, replace=T)
year = sample(2000:2011, n, replace=T)
destination = sample(LETTERS[1:6], n, replace=T)
`destination-year` = paste(destination, year, sep='-')
dat = data.frame(id, year, destination, `destination-year`)
现在将您的摘要制成表格。请注意我如何重新格式化为数据框并使名称与原始数据匹配。
incumbents = tapply(id, `destination-year`, function(x) length(unique(x)))
incumbents = data.frame(`destination-year`=names(incumbents), incumbents)
最后,与原始数据合并:
merge(dat, incumbents)
顺便说一句,不像您已经完成的那样将destination
and组合year
成第三个变量,tapply
可以直接将两个变量作为列表处理:
incumbents = melt(tapply(id, list(destination=destination, year=year), function(x) length(unique(x))))