It's about filter logic

According to my book, I try to practice . But I face a confused problem about typing order
library(magrittr)
library(dplyr)
abc<-diamonds%>%filter(cut%in%c('Ideal','Good'))
def<-diamonds%>%filter(cut%in%c('Good','Ideal'))
ABC<-filter(diamonds,cut==c('Ideal','Good'))
DEF<-filter(diamonds,cut==c('Good','Ideal'))
I mean the outputs are same between abc and def. when I type the last two, I get two datasets which are totally different.ABC includes 13229 observation and DEF inculdes 13228.And there is an interesting condition that the number of abc observations is 26457.I guess the abc's observations is separated into ABC&DEF by some unknown logic and the logic maybe has some relationship with the changed order.
Anyone can tell me reason?

Thanks!

When you use == instead of %in%, the comparison vector is recycled. The odd rows are checked against the first element and the even rows are checked against the second element. Look at this example.

library(dplyr)
df <- data.frame(cut = c("A", "A", "B", "C", "B", "C", "A", "B"), index = 1:8)
df
#>   cut index
#> 1   A     1
#> 2   A     2
#> 3   B     3
#> 4   C     4
#> 5   B     5
#> 6   C     6
#> 7   A     7
#> 8   B     8
AB <- filter(df, cut == c("A", "B"))
AB
#>   cut index
#> 1   A     1
#> 2   A     7
#> 3   B     8
BA <- filter(df, cut == c("B", "A"))
BA
#>   cut index
#> 1   A     2
#> 2   B     3
#> 3   B     5

Created on 2019-05-20 by the reprex package (v0.2.1)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.