dat=data.frame(
year=c(rep(2007,5),rep(2008,3),rep(2009,3)),
province=c("a","a","b","c","d","a","c","d","b","c","d"),
sale=1:11)
tapply(dat$sale,list(dat$year,dat$province),sum)
a b c d
2007 3 3 4 5
2008 6 NA 7 8
2009 NA 9 10 11
在这种情况下,如何将 tapply 更改为聚合以获得相同的结果?