user1754606
user1754606

Reputation: 1379

ddply and adding columns

I have a data frame with columns year|country|growth_rate. I wanted to to find country with highest growth rate in every year, which I did with:

ddply(data, .(year), summarise, highest=max(growth_rate))

and I've got data frame with 2 columns; year and highest

I would like to add third column here, which would show that country that had that max growth_rate, but I can't figure out how to do this.

Upvotes: 1

Views: 678

Answers (1)

Jake Burkhead
Jake Burkhead

Reputation: 6535

R> data = data.frame(year = rep(1990:1993, 2), growth_rate = runif(8), country = rep(c("US", "FR"), each = 4))
R> data
  year growth_rate country
1 1990  0.82785327      US
2 1991  0.86724498      US
3 1992  0.84813164      US
4 1993  0.35884355      US
5 1990  0.92792399      FR
6 1991  0.08659153      FR
7 1992  0.26732516      FR
8 1993  0.37819132      FR
R> ddply(data, .(year), summarize, highest = max(growth_rate), country = country[which.max(growth_rate)])
  year   highest country
1 1990 0.9279240      FR
2 1991 0.8672450      US
3 1992 0.8481316      US
4 1993 0.3781913      FR

Upvotes: 3

Related Questions