Alias Alias -4 years ago 46
R Question

Choose entities from data-frame where dummy-variable is 1 at least once

I have this data-frame:

df=data.frame(student=c(rep("John",6),rep("Meredith",7),rep("Jeremy",5),rep("Audrey",8)),
semester=c(1,2,3,4,5,6, 1,2,3,4,5,6,7, 1,2,3,4,5, 1,2,3,4,5,6,7,8),
addQual=c( 1,0,0,0,1,0, 0,0,0,0,0,0,0, 0,0,1,0,1, 0,0,0,0,0,0,0,0))


It contains students, all their semesters and for every semester whether they took an additional qualifications course(dummy variable addQual = 1 if they took it).
How can I get a data-frame dfFilt that only contains those students who ever participated in an additional qualifications course?

My desired output would therefore be:

dfFilt=data.frame(student=c(rep("John",6),rep("Jeremy",5)),
semester=c(1,2,3,4,5,6, 1,2,3,4,5),
addQual=c( 1,0,0,0,1,0, 0,0,1,0,1))


A solution in dplyr is preferred.

Answer Source

In dplyr:

dfFilt = df %>% group_by(student) %>% filter(sum(addQual)>=1)

   student semester addQual
    <fctr>    <dbl>   <dbl>
 1    John        1       1
 2    John        2       0
 3    John        3       0
 4    John        4       0
 5    John        5       1
 6    John        6       0
 7  Jeremy        1       0
 8  Jeremy        2       0
 9  Jeremy        3       1
10  Jeremy        4       0
11  Jeremy        5       1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download