如何有条件地选择/过滤R中每个组中的值

时间:2017-01-09 00:49:36

标签: r dplyr

我有一个数据框,我想通过Name和Acquired对变量进行分组,但只选择包含Position 10的组。

这是我的数据框的一个例子。

en.yml

输出应该是这样的:

           Name           Acquired       Position   Salary
1         Adam Dunn*      Amateur Draft        7   250000
2         Adam Dunn*      Amateur Draft        7   400000
3         Adam Dunn*      Amateur Draft        7   445000
4         Adam Dunn*      Amateur Draft        7  4600000
5         Adam Dunn*      Amateur Draft        7  7500000
6         Adam Dunn*      Amateur Draft        7 10500000
7         Adam Dunn*      Amateur Draft        7 13000000
8         Adam Dunn*        Free Agency        3  8000000
9         Adam Dunn*        Free Agency        3 12000000
10        Adam Dunn*        Free Agency       10 12000000
11        Adam Dunn*        Free Agency       10 14000000
12        Adam Dunn*        Free Agency       10 15000000
13        Adam Dunn*        Free Agency       10 15000000
14     Adam LaRoche*      Amateur Draft        3   300000
15     Adam LaRoche*      Amateur Draft        3   300000
16     Adam LaRoche*      Amateur Draft        3   337500
17     Adam LaRoche*      Amateur Draft        3   337500
18     Adam LaRoche*      Amateur Draft        3   420000
19     Adam LaRoche*      Amateur Draft        3   420000
20     Adam LaRoche*             Traded        3  3200000
21     Adam LaRoche*             Traded        3  5000000
22     Adam LaRoche*             Traded        3  7050000
23     Adam LaRoche*        Free Agency        3  4500000
24     Adam LaRoche*        Free Agency        3  7000000
25     Adam LaRoche*        Free Agency        3  8000000
26     Adam LaRoche*        Free Agency        3 10000000
27     Adam LaRoche*        Free Agency        3 12000000
28     Adam LaRoche*        Free Agency       10 12000000
29        Adam Lind*      Amateur Draft       10   411800
30        Adam Lind*      Amateur Draft       10   550000
31        Adam Lind*      Amateur Draft        3  5150000
32        Adam Lind*      Amateur Draft        3  5000000
33        Adam Lind*      Amateur Draft        3  5000000
34        Adam Lind*      Amateur Draft        3  7000000
35        Adam Lind*             Traded        3  7500000
36  Adrian Gonzalez*             Traded       10   316000
37  Adrian Gonzalez*             Traded        3   327500
38  Adrian Gonzalez*             Traded        3   500000
39  Adrian Gonzalez*             Traded        3   875000
40  Adrian Gonzalez*             Traded        3  3125000
41  Adrian Gonzalez*             Traded        3  4875000
42  Adrian Gonzalez*             Traded        3  6300000
43  Adrian Gonzalez*             Traded        3 21000000
44  Adrian Gonzalez*             Traded        3 21000000
45  Adrian Gonzalez*             Traded        3 21000000
46  Adrian Gonzalez*             Traded        3 21857000
47    Alan Bannister             Traded       10   350000
48      Albert Belle      Amateur Draft        9    68000
49      Albert Belle      Amateur Draft       10   117000
50      Albert Belle      Amateur Draft        7   130000
51      Albert Belle      Amateur Draft       10   175000
52      Albert Belle      Amateur Draft        7  1675000
53      Albert Belle      Amateur Draft        7  2775000
54      Albert Belle      Amateur Draft        7  4500000
55      Albert Belle      Amateur Draft        7  5700000
56      Albert Belle        Free Agency        7 10000000
57      Albert Belle        Free Agency        7 10000000
58     Albert Pujols      Amateur Draft        5   200000
59     Albert Pujols      Amateur Draft        7   600000
60     Albert Pujols      Amateur Draft        7   900000
61     Albert Pujols      Amateur Draft        3  7000000
62     Albert Pujols      Amateur Draft        3 11000000
63     Albert Pujols      Amateur Draft        3 14000000
64     Albert Pujols      Amateur Draft        3 12937813
65     Albert Pujols      Amateur Draft        3 13870949
66     Albert Pujols      Amateur Draft        3 14427326
67     Albert Pujols      Amateur Draft        3 14595953
68     Albert Pujols      Amateur Draft        3 14508395
69     Albert Pujols        Free Agency        3 12000000
70     Albert Pujols        Free Agency       10 16000000
71     Albert Pujols        Free Agency        3 23000000
72     Albert Pujols        Free Agency        3 24000000
73    Alex Rodriguez      Amateur Draft        6   442333
74    Alex Rodriguez      Amateur Draft        6   442333
75    Alex Rodriguez      Amateur Draft        6   442334
76    Alex Rodriguez      Amateur Draft        6  1062500
77    Alex Rodriguez      Amateur Draft        6  2162500
78    Alex Rodriguez      Amateur Draft        6  3112500
79    Alex Rodriguez      Amateur Draft        6  4362500
80    Alex Rodriguez        Free Agency        6 22000000
81    Alex Rodriguez        Free Agency        6 22000000
82    Alex Rodriguez        Free Agency        6 22000000
83    Alex Rodriguez             Traded        5 22000000
84    Alex Rodriguez             Traded        5 26000000
85    Alex Rodriguez             Traded        5 21680727
86    Alex Rodriguez        Free Agency        5 22708525
87    Alex Rodriguez        Free Agency        5 28000000
88    Alex Rodriguez        Free Agency        5 33000000
89    Alex Rodriguez        Free Agency        5 33000000
90    Alex Rodriguez        Free Agency        5 32000000
91    Alex Rodriguez        Free Agency        5 29000000
92    Alex Rodriguez        Free Agency        5 28000000
93    Alex Rodriguez             Traded       10 22000000
94   Alexi Amarista* Amateur Free Agent       10   481000
95   Alexi Amarista*             Traded        8   497400
96   Alexi Amarista*             Traded        6   511100
97   Alexi Amarista*             Traded        6  1150000
98       Allen Craig      Amateur Draft        9   400000
99       Allen Craig      Amateur Draft        7   414000
100      Allen Craig      Amateur Draft        3   495000

基本上,如果我想按组选择10位置,我只需要子集,或者编写这段代码:

           Name           Acquired       Position   Salary
8         Adam Dunn*        Free Agency        3  8000000
9         Adam Dunn*        Free Agency        3 12000000
10        Adam Dunn*        Free Agency       10 12000000
11        Adam Dunn*        Free Agency       10 14000000
12        Adam Dunn*        Free Agency       10 15000000
13        Adam Dunn*        Free Agency       10 15000000
23     Adam LaRoche*        Free Agency        3  4500000
24     Adam LaRoche*        Free Agency        3  7000000
25     Adam LaRoche*        Free Agency        3  8000000
26     Adam LaRoche*        Free Agency        3 10000000
27     Adam LaRoche*        Free Agency        3 12000000
28     Adam LaRoche*        Free Agency       10 12000000
29        Adam Lind*      Amateur Draft       10   411800
30        Adam Lind*      Amateur Draft       10   550000
31        Adam Lind*      Amateur Draft        3  5150000
32        Adam Lind*      Amateur Draft        3  5000000
33        Adam Lind*      Amateur Draft        3  5000000
34        Adam Lind*      Amateur Draft        3  7000000
35        Adam Lind*             Traded        3  7500000
36  Adrian Gonzalez*             Traded       10   316000
37  Adrian Gonzalez*             Traded        3   327500
38  Adrian Gonzalez*             Traded        3   500000
39  Adrian Gonzalez*             Traded        3   875000
40  Adrian Gonzalez*             Traded        3  3125000
41  Adrian Gonzalez*             Traded        3  4875000
42  Adrian Gonzalez*             Traded        3  6300000
43  Adrian Gonzalez*             Traded        3 21000000
44  Adrian Gonzalez*             Traded        3 21000000
45  Adrian Gonzalez*             Traded        3 21000000
46  Adrian Gonzalez*             Traded        3 21857000
47    Alan Bannister             Traded       10   350000
48      Albert Belle      Amateur Draft        9    68000
49      Albert Belle      Amateur Draft       10   117000
50      Albert Belle      Amateur Draft        7   130000
51      Albert Belle      Amateur Draft       10   175000
52      Albert Belle      Amateur Draft        7  1675000
53      Albert Belle      Amateur Draft        7  2775000
54      Albert Belle      Amateur Draft        7  4500000
55      Albert Belle      Amateur Draft        7  5700000
69     Albert Pujols        Free Agency        3 12000000
70     Albert Pujols        Free Agency       10 16000000
71     Albert Pujols        Free Agency        3 23000000
72     Albert Pujols        Free Agency        3 24000000
93    Alex Rodriguez             Traded       10 22000000
94   Alexi Amarista* Amateur Free Agent       10   481000

我想保留每个包含10个组的组,并过滤​​掉每个没有组的组。因此,业余选秀获得的亚当邓恩已经出局,但是亚当邓恩自由球员已经出局了。我怀疑这会涉及某种条件过滤,但我不确定它是什么。

1 个答案:

答案 0 :(得分:2)

为了对组进行过滤,一个选项是每组返回一个唯一的布尔值,然后由于广播将用于选择整个组;对于您的情况,围绕条件包裹any应该有效:

df %>% group_by(Name, Acquired) %>% filter(any(Position == 10)) %>% as.data.frame()