Summing by Categorical Variable
I have a data set of comic book unit sales by volume (ex. Naruto v10) that I need to reduce to sales by series (so all Naruto volume unit sales would be added together into a single observation). I have a variable "series" that identifies开发者_StackOverflow the series of each observation. The equivalent code in Stata would be:
by series, sort:replace unitssales=sum(unitssales);
by series, sort:keep if _n==_N
But I'm trying to figure out how to do this in R. Any help would be much appreciated! Thanks in advance!
Without knowing what format your data is in, I can only suggest you look at the tapply
function. From the help:
> n <- 17; fac <- factor(rep(1:3, length = n), levels = 1:5)
> tapply(1:n, fac, sum)
1 2 3 4 5
51 57 45 NA NA
精彩评论