首页 文章

Dplyr:总结,变异和排名

提问于
浏览
2

当我在 mtcars 数据集上执行以下查询时,我得到以下结果 .

mtcars %>% 
   group_by(cyl,gear) %>% 
   summarise(total_cnt = n(), totalwt = sum(wt)) %>% 
   arrange(cyl, gear, desc(total_cnt), desc(totalwt)) %>% 
   mutate(rank = dense_rank(desc(total_cnt))) %>% 
   arrange(rank)

 cyl  gear total totalwt  rank
  <dbl> <dbl> <int>   <dbl> <int>
1     4     4     8  19.025     1
2     6     4     4  12.375     1
3     8     3    12  49.249     1
4     4     5     2   3.653     2
5     6     3     2   6.675     2
6     8     5     2   6.740     2
7     4     3     1   2.465     3
8     6     5     1   2.770     3

现在在每个组(排名)中,我想基于 totalwt 对观察进行子排名,因此最终输出应该看起来像(每个排名组中的 totalwt 的desc顺序)

cyl  gear total_cnt totalwt  rank    subrank
  <dbl> <dbl>     <int>   <dbl> <int>   <int>
1     4     4         8  19.025     1    2
2     6     4         4  12.375     1    3
3     8     3        12  49.249     1    1
4     4     5         2   3.653     2    3
5     6     3         2   6.675     2    2
6     8     5         2   6.740     2    1
7     4     3         1   2.465     3    2
8     6     5         1   2.770     3    1

然后最后排名前1,其中每个排名的子排名= 1,所以输出将是:

cyl  gear total_cnt totalwt  rank    subrank
  <dbl> <dbl>     <int>   <dbl> <int>   <int>
3     8     3        12  49.249     1    1
6     8     5         2   6.740     2    1
8     6     5         1   2.770     3    1

1 回答

  • 3

    如果从OP的代码输出'mtcars1',我们可以使用 rank 在'rank'分组后创建'subrank'

    mtcars2 <- mtcars1 %>%
                   group_by(rank) %>%
                   mutate(subrank = rank(-totalwt))
    mtcars2
    #    cyl  gear total_cnt totalwt  rank subrank
    #   <dbl> <dbl>     <int>   <dbl> <int>   <dbl>
    #1     4     4         8  19.025     1       2
    #2     6     4         4  12.375     1       3
    #3     8     3        12  49.249     1       1
    #4     4     5         2   3.653     2       3
    #5     6     3         2   6.675     2       2
    #6     8     5         2   6.740     2       1
    #7     4     3         1   2.465     3       2
    #8     6     5         1   2.770     3       1
    

    然后,我们 filter 'subrank'为1的行

    mtcars2 %>% 
          filter(subrank ==1)
    #    cyl  gear total_cnt totalwt  rank subrank
    #   <dbl> <dbl>     <int>   <dbl> <int>   <dbl>
    #1     8     3        12  49.249     1       1
    #2     8     5         2   6.740     2       1
    #3     6     5         1   2.770     3       1
    

相关问题