Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr filter function gives wrong data

Tags:

r

filter

dplyr

I have the following dataset: (sample)

    Team    Job Question    Answer
1   1        1     2             1
2   1        1     3a            2
3   1        1     3b            2
4   1        1     4a            1
5   1        1     4b            1

and I have 21 teams so there are many rows. I am trying filter the rows of the teams which did good in the experiment (with the dplyr package):

q10best <- filter(quest,Team==c(2,4,6,10,13,17,21))

But it gives me messed up data and with many missing rows. On the other hand, when I use:

q10best <- filter(quest,Team==2 | Team==4 | Team==6 | Team==10 | Team==13 | Team==17 | Team==21)

It gives me the right dataset that I want. What is the difference? what am I doing wrong in the first command? Thanks

like image 256
Michael Kozak Avatar asked Sep 26 '26 07:09

Michael Kozak


1 Answers

== checks if two objects are exactly the same. You are trying to check if one object (each element of quest$Team) belongs to a list of value. The proper way to do that is to use %in%

q10best <- filter(quest,Team %in% c(2,4,6,10,13,17,21))
like image 140
scoa Avatar answered Sep 28 '26 20:09

scoa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!