Correct typo or explanation (#818)

11 | 12 should be enclosed in parentheses otherwise the explanation is incorrect (Issue #704)
This commit is contained in:
robertchu03 2020-01-16 02:23:51 +08:00 committed by Hadley Wickham
parent e8e0218f1a
commit 80aec62b56
1 changed files with 1 additions and 1 deletions

View File

@ -125,7 +125,7 @@ The following code finds all flights that departed in November or December:
filter(flights, month == 11 | month == 12)
```
The order of operations doesn't work like English. You can't write `filter(flights, month == 11 | 12)`, which you might literally translate into "finds all flights that departed in November or December". Instead it finds all months that equal `11 | 12`, an expression that evaluates to `TRUE`. In a numeric context (like here), `TRUE` becomes one, so this finds all flights in January, not November or December. This is quite confusing!
The order of operations doesn't work like English. You can't write `filter(flights, month == (11 | 12))`, which you might literally translate into "finds all flights that departed in November or December". Instead it finds all months that equal `11 | 12`, an expression that evaluates to `TRUE`. In a numeric context (like here), `TRUE` becomes one, so this finds all flights in January, not November or December. This is quite confusing!
A useful short-hand for this problem is `x %in% y`. This will select every row where `x` is one of the values in `y`. We could use it to rewrite the code above: