开发者

Summing by Categorical Variable

开发者 https://www.devze.com 2022-12-10 05:30 出处:网络
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 hav

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 
0

精彩评论

暂无评论...
验证码 换一张
取 消