Will Hong
Will Hong

Reputation: 53

What is the difference between `&` v.s. `,` when using dplyr::filter?

I've read some dplyr vignettes and dplyr::filter example, I'm curious does there has any difference between & and , when subset rows using column values? Because the result is identical, I just afraid could make some semantic errors. Below is paste directly from the example:

filter(starwars, hair_color == "none" & eye_color == "black")

filter(starwars, hair_color == "none", eye_color == "black")

# A tibble: 9 × 14
  name       height  mass hair_color skin_color    eye_color birth_year sex   gender
  <chr>       <int> <dbl> <chr>      <chr>         <chr>          <dbl> <chr> <chr> 
1 Nien Nunb     160    68 none       grey          black             NA male  mascu…
2 Gasgano       122    NA none       white, blue   black             NA male  mascu…
3 Kit Fisto     196    87 none       green         black             NA male  mascu…
... with more entries and variables ...

And could I put them together? Using & , within a single expression? Like below:

filter(starwars, hair_color == "none" & eye_color == "black", gender == "masculine")

filter(starwars, hair_color == "none", eye_color == "black" & gender == "masculine")

# A tibble: 7 × 14
  name       height  mass hair_color skin_color  eye_color birth_year sex   gender  
  <chr>       <int> <dbl> <chr>      <chr>       <chr>          <dbl> <chr> <chr>   
1 Nien Nunb     160    68 none       grey        black             NA male  masculi…
2 Gasgano       122    NA none       white, blue black             NA male  masculi…
3 Kit Fisto     196    87 none       green       black             NA male  masculi…
... with more entries and variables ...

Because the result is also identical, thanks in advance!

Upvotes: 5

Views: 832

Answers (1)

bretauv
bretauv

Reputation: 8567

From the documentation (?dplyr::filter):

If multiple expressions are included, they are combined with the & operator.

Therefore, using , and & is identical.


This answer comes from @RitchieSacramento's comment and was posted a month after. I don't want to steal credit, I just want this question to stop appearing as "unanswered" while it is actually answered.

Upvotes: 1

Related Questions