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.