dplyr - 分组并选择TOP x% [英] dplyr - Group by and select TOP x %

查看:98
本文介绍了dplyr - 分组并选择TOP x%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用软件包dplyr和函数 sample_frac 可以对每个组中的百分比进行抽样。我需要的是首先对每个组中的元素进行排序,然后从每个组中选择顶部x%。

Using the package dplyr and the function sample_frac it is possible to sample a percentage from every group. What I need is to first sort the elements in every group and then select top x% from every group?

有一个函数 top_n ,但这里我只能确定行数,我需要一个相对值。

There is a function top_n, but here I can only determine the number of rows, and I need a relative value.

例如,以下数据按齿轮分组每个组中的 wt

For example the following data is grouped by gear and sorted by wt within each group:

library(dplyr)
mtcars %>%
  select(gear, wt) %>%
  group_by(gear) %>%
  arrange(gear, wt)

    gear    wt
1   3   2.465
2   3   3.215
3   3   3.435
4   3   3.440
5   3   3.460
6   3   3.520
7   3   3.570
8   3   3.730
9   3   3.780
10  3   3.840
11  3   3.845
12  3   4.070
13  3   5.250
14  3   5.345
15  3   5.424
16  4   1.615
17  4   1.835
18  4   1.935
19  4   2.200
20  4   2.320
21  4   2.620
22  4   2.780
23  4   2.875
24  4   3.150
25  4   3.190
26  4   3.440
27  4   3.440
28  5   1.513
29  5   2.140
30  5   2.770
31  5   3.170
32  5   3.570

现在我想选择每个齿轮组中的前20%。

Now I would like to select top 20 % within each gear group.

如果解决方案可以与dplyr的 group_by 函数集成,那将是非常好的。

It would be very nice if the solution could be integrated with dplyr's group_by function.

推荐答案

或dplyr的其他选项:

Or another option with dplyr:

mtcars %>% select(gear, wt) %>% 
  group_by(gear) %>% 
  arrange(gear, desc(wt)) %>% 
  filter(wt > quantile(wt, .8))

Source: local data frame [7 x 2]
Groups: gear [3]

   gear    wt
  (dbl) (dbl)
1     3 5.424
2     3 5.345
3     3 5.250
4     4 3.440
5     4 3.440
6     4 3.190
7     5 3.570

这篇关于dplyr - 分组并选择TOP x%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆