2

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 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!

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
  • This is a duplicate of the question rcs identified: http://stackoverflow.com/questions/1660124/how-to-group-columns-by-sum-in-r. It's also almost identical to this question: http://stackoverflow.com/questions/1407449/for-each-group-summarise-means-for-all-variables-in-dataframe-ddply-split. – Shane Nov 03 '09 at 14:12

1 Answers1

2

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 
Jonathan Chang
  • 24,567
  • 5
  • 34
  • 33