# Logical operators


Find this notebook on the web at
<a class="quarto-xref" href="https://resampling-stats.github.io/latest-r/testing_counts_2.html#nte-logical_operators">Note <span>23.1</span></a>.

This section continues our programme of expanding the range of R
features that you can use to clear code. As we introduce each feature,
we will use them in the following examples.

As motivation, we are about to do some simulations where we are
interested in the number of some particular type of observations in each
trial. For example, let’s do 10 coin tosses with `sample`:

```{r}
# For each element, heads of tails is equally likely.
coins <- sample(c('heads', 'tails'), size=10, replace=TRUE)
coins
```

Let us now say that we are interested to record if the trial had
*either* 2 or fewer “heads” *or* two or fewer “tails”.

We could write it like this:

```{r}
if (sum(coins == 'heads') <= 2) {
    message('Trial is of interest')
}
```

```{r}
if (sum(coins == 'tails') <= 2) {
    message('Trial is of interest')
}
```

It is a little repetitive to have to repeat the code identical code to
print the same message for either of the two cases, and it would be even
more repetitive if there were more lines of identical code to run for
each of the two cases.

R solves this problem with the `|` (*or*) operator, like this:

```{r}
if (sum(coins == 'heads') <= 2 | sum(coins == 'tails') <= 2) {
    message('Trial is of interest')
}
```

<div __quarto_custom="true" __quarto_custom_context="Block" __quarto_custom_id="35" __quarto_custom_type="Callout">
<div __quarto_custom_scaffold="true">

What is an operator?

</div>
<div __quarto_custom_scaffold="true">

Above, we called `|` an *operator*. An operator, for our purposes, is a
special character that sits between two values, and that tells R how to
combine these values.

For example `+` is an operator. When `+` sits between two numbers in
code, R interprets this to mean “take the two numbers on either side,
and make a new number that is the result of adding the two numbers”:</div></div>

```{r}
# + is an operator that, between two numbers, means "add the numbers".
1 + 3
```

`+`, `-`, `/` and `*` are all examples of operators that do *arithmetic*
on the numbers to either side — they are *arithmetic* operators.

```{r}
# * is an operator that, between two numbers, means "multiply the numbers".
2 * 4
```





We are about to use the operator `|` (*or*). `|` is a *logical*
operator. It is a logical operator because it does not operate on
*numbers* (as arithmetic operators do), but on *logical* (*Boolean*)
values — values that can be either `TRUE` or `FALSE`.

For example, here we use `|` (*or*). to combine a `TRUE` value (on the
left) with a `FALSE` value (on the right). It gives a result — `TRUE`.

```{r}
# Read "|" here as "or".
TRUE | FALSE
```

`|` (*or*) applies a very simple rule: if *either* the left-hand (LH)
*or* the right-hand (RH) values are `TRUE`, then `|` (*or*) evaluates to
`TRUE`. Only if *neither* of the LH and RH values are `TRUE`, does it
return `FALSE`.

```{r}
# Both LH and RH are TRUE, return TRUE.
message('TRUE | (or) TRUE result: ', TRUE | TRUE)
```

```{r}
# Only LH is TRUE, return TRUE.
message('TRUE | (or) FALSE result: ', TRUE | FALSE)
```

```{r}
# Only RH is TRUE, return TRUE.
message('FALSE | (or) TRUE result: ', FALSE | TRUE)
```

```{r}
# Neither LH nor RH are TRUE, return FALSE.
message('FALSE | (or) FALSE result: ', FALSE | FALSE)
```

Now let’s go back to the `if` statement above. The conditional part of
the header line is:

```{r}
sum(coins == 'heads') <=2 | sum(coins == 'tails') <= 2
```

This will be `TRUE` *either* when there there are two or fewer “heads”,
*or* when there are two or fewer tails. Therefore, when we use this
conditional in an `if` statement, we make the *body* of the `if`
statement run only if either of the two conditions are `TRUE`.

```{r}
if (sum(coins == 'heads') <= 2 | sum(coins == 'tails') <= 2) {
    message('Trial is of interest')
}
```

While we are here, R has another very useful logical operator: ‘&amp;’
(*and*).

‘&amp;’ takes the LH and RH values, and returns `TRUE` only if *both* values
are `TRUE`.

```{r}
# Both LH and RH are TRUE, return TRUE.
message('TRUE & (and) TRUE result: ', TRUE & TRUE)
```

```{r}
# Only LH is TRUE, return FALSE.
message('TRUE & (and) FALSE result: ', TRUE & FALSE)
```

```{r}
# Only RH is TRUE, return FALSE.
message('FALSE & (and) TRUE result: ', FALSE & TRUE)
```

```{r}
# Neither LH nor RH are TRUE, return FALSE.
message('FALSE & (and) FALSE result: ', FALSE & FALSE)
```

We could, for example, ask whether the number of heads is \&gt;=3 *and*
\&lt;=7 (is in the range 3 through 7).

```{r}
if (sum(coins == 'heads') <= 3 | sum(coins == 'heads') <= 7) {
    message('Trial is of interest')
}
```

