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.
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