filter in dplyr using a categorical variable

7

Suppose I have the following dataset:

set.seed(12)
dados <- data.frame(grupos=rep(letters[1:5], 5), valores=rnorm(25))
head(dados)
  grupos    valores
1      a -1.8323176
2      b -0.0560389
3      c  0.6692396
4      d  0.8067977
5      e  0.2374370
6      a  0.7894452

How can I filter only rows whose groups are equal to a or b ? I know how to filter the rows equal to one level:

library(dplyr)

dados %>%
  filter(grupos=="a")
  grupos    valores
1      a -1.8323176
2      a  0.7894452
3      a -0.9993864
4      a  0.3844801
5      a -1.3305330

dados %>%
  filter(grupos=="b")
  grupos     valores
1      b -0.05603890
2      b  0.37511302
3      b -0.03578014
4      b  0.65215941
5      b  1.64394981

I could individually do each of the filters and put them together later. However, my original problem is more complicated because it is a data frame with 26,691 lines, where I must filter 1,116 different values. It is impractical to filter out each of these values individually and then combine them at the end.

    
asked by anonymous 08.06.2017 / 18:15

1 answer

3

In this case you can use %in% :

dados %>%
  filter(grupos %in% c("a", "b"))
   grupos valores
1       a -1.4806
2       b  1.5772
3       a -0.2723
4       b -0.3153
5       a -0.7777
6       b -1.2939
7       a -0.7035
8       b  1.1889
9       a  0.2236
10      b  2.0072
    
10.06.2017 / 09:42