【发布时间】:2018-04-23 19:09:59
【问题描述】:
使用 Dplyr,我试图从以下数据中找出 2002 年至 2006 年间哪个国家的财富增长幅度最大。
Country wealth_2002 wealth_2004 wealth_2006
Country_A 1000 1600 2200
Country_B 1200 1300 1800
Country_C 1400 1100 1200
Country_D 1500 1000 1100
Country_E 1100 1800 1900
为了得到国家的名字,我用过
largest_increase <- df %>%
group_by(Country) %>%
filter(max(wealth_2006 - wealth_2002)) %>%
这给了我
Error in filter_impl(.data, quo) :
Argument 2 filter condition does not evaluate to a logical vector
如果有人能帮助我解决我做错了什么以及如何解决这个问题,我将不胜感激。我对 R 非常陌生,因此我们将不胜感激。
【问题讨论】:
-
试试
filter(row_number() == which.max(...)) -
slice(which.max(...))是你所需要的 -
您可以只排序:
df %>% arrange(desc(wealth_2006 - wealth_2002))。如果您愿意,请先添加一列:df %>% mutate(change = wealth_2006 - wealth_2002) %>% arrange(desc(change))