【问题标题】:Ranking within multiple groups - R在多个组内排名 - R
【发布时间】: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


    【解决方案1】:

    这对你有帮助吗?

    library(tibble)
    library(dplyr)
    
    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 = tibble(tech, country, year, value)
    df
    #> # A tibble: 24 x 4
    #>    tech  country  year value
    #>    <chr> <chr>   <dbl> <dbl>
    #>  1 cars  Austria  2010    42
    #>  2 cars  Austria  2011    23
    #>  3 cars  Austria  2012    13
    #>  4 cars  Austria  2013    13
    #>  5 cars  Brazil   2010   646
    #>  6 cars  Brazil   2011   454
    #>  7 cars  Brazil   2012    23
    #>  8 cars  Brazil   2013   234
    #>  9 cars  USA      2010    12
    #> 10 cars  USA      2011   123
    #> # … with 14 more rows
    
    df %>% 
      group_by(tech, year) %>% 
      mutate(rank = rank(value)) %>% 
      arrange(tech, year, rank)
    #> # A tibble: 24 x 5
    #> # Groups:   tech, year [8]
    #>    tech  country  year value  rank
    #>    <chr> <chr>   <dbl> <dbl> <dbl>
    #>  1 cars  USA      2010    12     1
    #>  2 cars  Austria  2010    42     2
    #>  3 cars  Brazil   2010   646     3
    #>  4 cars  Austria  2011    23     1
    #>  5 cars  USA      2011   123     2
    #>  6 cars  Brazil   2011   454     3
    #>  7 cars  USA      2012     1     1
    #>  8 cars  Austria  2012    13     2
    #>  9 cars  Brazil   2012    23     3
    #> 10 cars  Austria  2013    13     1
    #> # … with 14 more rows
    

    reprex package (v0.3.0) 于 2020 年 3 月 20 日创建

    【讨论】:

    • 嗨,大卫,感谢您的快速回复。我正在寻找技术和年份中国家/地区的排名,因此由于只有 3 个国家,因此排名只能是 3 高。不知道你的排名是多少? IE。在汽车和 2010 年,美国排名第一,巴西排名第三
    • 我已更新代码以将 tech 包含在分组变量中...
    • 之前 (group_by(year)) 仅按年份分组,因此我们总共有 6 个案例
    • 完美,正是我想要的。我会尽快将您的答案标记为已接受。我真的很感激:)
    猜你喜欢
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多