0

I have data.frame with 100k rows and lots of variables. One variable is "Country", which shows one of 17 countries. Another variable is "Mass".

I also have a vector that lists the 17 unique countries. I'm trying to create a vector that shows the sum of "Mass" for each Country - i.e. a list of 17 numbers showing the total Mass for each Country.

I can do the conditional sum for one specific country, but I can't work out how to create the vector.

Any help is greatly appreciated - many thanks.

Dan Lewer
  • 871
  • 5
  • 12
  • 2
    Welcome to StackOverflow! We expect more from questions here. You aren't going to get any help by simply describing a problem and waiting for people to solve it for you. We expect questions to contain, at the _very_ least, that you've made a serious attempt, and that you _show_ us that attempt. This should take the form of a [reproducible](http://stackoverflow.com/q/5963269/324364) example. – joran Nov 29 '13 at 00:31
  • ?aggregate will give what you want. – Pierre Lapointe Nov 29 '13 at 00:36

1 Answers1

0

Since you did not provide a reproducible example, I'll use the iris dataset.

aggregate(iris[,1:4], by=list(iris$Species), sum)
#     Group.1 Sepal.Length Sepal.Width Petal.Length Petal.Width
#1     setosa        250.3       171.4         73.1        12.3
#2 versicolor        296.8       138.5        213.0        66.3
#3  virginica        329.4       148.7        277.6       101.3
Pierre Lapointe
  • 16,017
  • 2
  • 43
  • 56