我有一个包含year | country | growth_rate列的数据框。我想找到每年增长率最高的国家,我这样做了:
ddply(data, .(year), summarise, highest=max(growth_rate))
我有2列数据框;年和最高
我想在这里添加第三列,这将显示具有最大growth_rate的国家/地区,但我无法弄清楚如何执行此操作。
答案 0 :(得分:3)
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