Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to conditionally select/filter values in each group in R

Tags:

r

dplyr

I have a data frame, where I want to group variables by Name and Acquired, but only select the groups which contain a Position of 10.

here is an example of my data frame.

           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

The output should be something like this:

           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

Basically, if I wanted to select just the position 10 by group I would just subset, or write this code:

library(dplyr)

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

I want to keep every group that has a 10 in it, and filter out every group that doesn't. So, Adam Dunn Acquired by Amateur Draft is out but Adam Dunn Free Agency is in. I suspect that this would involve some sort of conditional filtering but I am not sure what it is.

like image 705
Julien Avatar asked Jan 09 '17 00:01

Julien


1 Answers

In order to filter on groups, one option is to return a unique boolean value per group, which will then be used to select the entire group due to broadcasting; For your case, wrap any around the condition should work:

df %>% group_by(Name, Acquired) %>% filter(any(Position == 10)) %>% as.data.frame()
like image 130
Psidom Avatar answered Oct 07 '22 18:10

Psidom