Skip to content

Commit

Permalink
adding extra slides about nuance of filter order
Browse files Browse the repository at this point in the history
  • Loading branch information
carriewright11 committed Dec 6, 2024
1 parent 7756712 commit 46d9a73
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions modules/Subsetting_Data_in_R/Subsetting_Data_in_R.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,30 @@ select(er_30, starts_with(c("r", "l"))) # here we combine two patterns
```

## Nuances about `filter()`

```{r}
test <- tibble(A = c(1,2,3,4), B = c(1,2,3,4))
test
# These are technically the same but >= is easier to read
# Separating can cause issues
filter(test, B > 2 | B==2)
filter(test, B >= 2)
```

## Order of operations for `filter()`

Order can matter. Think of individual statements separately first.
```{r}
filter(test, A>3 | B==2 & B>2) # A is greater than 3 or B is equal to 2 AND (think but also) B must be greater than 2 , thus 2 is dropped.
filter(test, A>3 & B>2 | B==2) # A is greater than 3 AND B is greater than 2 leaving only 4s OR B is equal to 2, (since this comes later, 2 is preserved)
```




## Ordering the column names of a data frame: alphabetically {.codesmall}

Using the base R `order()` function.
Expand Down

0 comments on commit 46d9a73

Please sign in to comment.