当我在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
的排序顺序)
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
答案 0 :(得分:3)
如果&#39; mtcars1&#39;从OP的代码输出,我们可以使用event_params.reject { |k, v| k.starts_with? 'starts_at' }.merge(starts_at: nil)
创建&#39; subrank&#39;按照排名&#39;
rank
然后,我们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
行&#39; subrank&#39;是1
filter