【发布时间】:2020-03-20 18:39:06
【问题描述】:
我有一个这样的数据框:
country = c("Austria", "Austria","Austria","Austria", "Brazil", "Brazil", "Brazil", "Brazil", "USA", "USA", "USA", "USA",
"Austria", "Austria","Austria","Austria", "Brazil", "Brazil", "Brazil", "Brazil", "USA", "USA", "USA", "USA")
tech = c("cars", "cars","cars","cars","cars","cars","cars","cars","cars","cars","cars","cars","planes","planes","planes",
"planes","planes","planes","planes","planes","planes","planes","planes","planes")
year = c(2010, 2011, 2012, 2013, 2010, 2011, 2012, 2013, 2010, 2011, 2012, 2013,2010, 2011, 2012, 2013, 2010, 2011, 2012,
2013, 2010, 2011, 2012, 2013)
value = c(42, 23, 13, 13, 646,454, 23, 234, 12, 123, 1, 23, 23, 54, 2, 77, 584, 66, 767, 6767, 23, 12, 12, 99)
df = data.frame(tech, country, year, value)
我想要一个新列,其中我使用这些值按年份对每个技术中的国家/地区进行排名。
所以,在 2013 年,对于飞机,我想说“美国”排名第一(具有当年和技术最高的价值)。
我知道如何在一组中排名,例如如果我没有技术,我想按国家和年份排名,我会这样做:
df = df %>%
group_by(year) %>%
arrange(country) %>%
mutate(`whatever` = order(order(value, decreasing = TRUE)))
但是,我不知道如何将技术添加到组合中,即各国每年的技术排名。
有人有指导吗?
【问题讨论】:
标签: r