Use of () inside a multi-conditional filter

Hey there,

I'm trying to wrap my head around the use and the meaning of parentheses inside dplyr::filter() function. For instance, is there a difference between:

mtcars %>% filter(gear == 4 | carb == 1 & mpg > 20)

and

mtcars %>% filter(gear == 4 | (carb == 1 & mpg > 20))

?

What is the purpose of the parentheses? I tried to search for some documentation on this but was not able to find it. Maybe you could point me to some readings on this.

Thank you!

The parens are there so that the within expression is evaluated first. Because operator precedence.

1 Like

In this example, there is no difference but there can be a difference and parentheses can be used to change an order of precedence. Another reason to use them is to make easier for the humans to understand even when the logic is the same.

See some examples below:

library(tidyverse)

slim_mtcars <- tribble(
  ~gear, ~carb, ~mpg,
  4, 1, 21,
  4, 1, 19,
  4, 0, 21,
  4, 0, 19,
  3, 1, 21,
  3, 1, 19,
  3, 0, 21,
  3, 0, 19
)

slim_mtcars %>% filter(gear == 4 | carb == 1 & mpg > 20)
#> # A tibble: 5 × 3
#>    gear  carb   mpg
#>   <dbl> <dbl> <dbl>
#> 1     4     1    21
#> 2     4     1    19
#> 3     4     0    21
#> 4     4     0    19
#> 5     3     1    21
# this can be thought of as finding rows that first have gear==4 or carb==1
# and then of those which also have mpg >20
# that is the order of operations, left to right

slim_mtcars %>% filter(gear == 4 | (carb == 1 & mpg > 20))
#> # A tibble: 5 × 3
#>    gear  carb   mpg
#>   <dbl> <dbl> <dbl>
#> 1     4     1    21
#> 2     4     1    19
#> 3     4     0    21
#> 4     4     0    19
#> 5     3     1    21

# this is thought of those that have gear == 4 or those that have both carb==1 and mpg>20



slim_mtcars %>% filter(gear == 4 & (carb == 1 | mpg > 20))
#> # A tibble: 3 × 3
#>    gear  carb   mpg
#>   <dbl> <dbl> <dbl>
#> 1     4     1    21
#> 2     4     1    19
#> 3     4     0    21
# this is thought of those that have gear == 4 AND those that have both carb==1 and mpg>20
slim_mtcars %>% filter(gear == 4 & carb == 1 | mpg > 20)
#> # A tibble: 5 × 3
#>    gear  carb   mpg
#>   <dbl> <dbl> <dbl>
#> 1     4     1    21
#> 2     4     1    19
#> 3     4     0    21
#> 4     3     1    21
#> 5     3     0    21
# this is thought of those that have gear ==4 and carb==1 OR mpg>20 which ends up being different

Created on 2022-10-18 with reprex v2.0.2

2 Likes

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.