【问题标题】:Data Wrangling Using Dplyr使用 Dplyr 进行数据整理
【发布时间】: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 %&gt;% arrange(desc(wealth_2006 - wealth_2002))。如果您愿意,请先添加一列:df %&gt;% mutate(change = wealth_2006 - wealth_2002) %&gt;% arrange(desc(change))

标签: r dplyr


【解决方案1】:

使用 Base R,您可以使用 which.max 为您的国家/地区列建立索引:

# This is my dummy data, you can ignore it
country <- c("Sweden", "Finland")
X1 <- c(1050, 1067)
X2 <- c(1045, 1069)
DF <- data.frame(country, X1, X2)
# Modify this to suit
DF$country[which.max(DF$X2- DF$X1)]

所以对你来说应该是:

df$Country[which.max(df$wealth_2006 - df$wealth_2002)]

【讨论】:

  • 我已经使用 base R 展示了它,因为您是 R 新手,我想强调一下熟悉使用 base R 是多么重要,它将帮助您解决问题解决
【解决方案2】:

看看filter 是如何工作的——您需要为每一行提供一个逻辑“测试”,如果通过,它将保留该行。也不需要group_by国家,因为每个国家都已经是自己的行了。试试这样的方法,计算并存储每个国家/地区的财富变化,然后保留具有该最大值的国家/地区:

library(dplyr)

df <- read.table(
  text = "
  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
  ", header = TRUE, stringsAsFactors = FALSE
)

df %>%
  mutate(wealth_change = wealth_2006 - wealth_2002) %>% 
  filter(wealth_change == max(wealth_change)) %>% 
  pull(Country) # gives us the Country column

输出:

[1] "Country_A"

【讨论】:

    【解决方案3】:

    使用dput(data) 帮助解答。

    structure(list(Country = structure(1:5, .Label = c("Country_A", 
    "Country_B", "Country_C", "Country_D", "Country_E"), class = "factor"), 
        wealth_2002 = c(1000L, 1200L, 1400L, 1500L, 1100L), wealth_2004 = c(1600L, 
        1300L, 1100L, 1000L, 1800L), wealth_2006 = c(2200L, 1800L, 
        1200L, 1100L, 1900L)), .Names = c("Country", "wealth_2002", 
    "wealth_2004", "wealth_2006"), class = "data.frame", row.names = c(NA, 
    -5L))
    
    library(dplyr)
    data %>% 
    mutate(delta = wealth_2006 - wealth_2004) %>% #Create a new variable called delta with mutate
    arrange(desc(delta)) %>% #sort descending by 'delta' 
    head(1) #return the top line.. pull out the specific value if needed
    

    这将返回第一行……最大的变化。

    A国变化600

    【讨论】:

      【解决方案4】:

      你也可以使用top_n

      library(dplyr)
      df %>% top_n(1,wealth_2006 - wealth_2002)
      
      #     Country wealth_2002 wealth_2004 wealth_2006
      # 1 Country_A        1000        1600        2200
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-17
        • 2019-06-29
        • 2018-07-25
        • 2020-06-11
        • 2019-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多