-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathR04_logic.qmd
126 lines (75 loc) · 3.21 KB
/
R04_logic.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Logical Operators
Logical Operators are essential to efficiently work with R. It is a good idea to spent some time with them and think about their possibilities and usage.
But first: **What are logical operators?** They are used to compare two values. The most intuitive ones are the `<` and `>` operators, for `less than` and `larger than` respectively.
```{r}
4 < 5
6 > 2
```
As you see, both statements return the value `TRUE` because, --- well --- 4 is smaller than 5 and 6 is larger than 2. As you might guess, if there is `TRUE` there should also be a `FALSE`.
```{r}
24 > 50
```
Logical operators will **always** return `TRUE` or `FALSE`, so the statements you make with them are always questions which you can answer as `TRUE` or `FALSE`. As a data type, this is called a `logical` (or sometimes `boolean` after the British mathematician George Boole).
```{r}
bool <- 24 < 50
class(bool)
```
To ask the question if two values are the same we use the operator `==`. Be careful to not confuse this with the assignment operator `=`.
```{r}
1.9999999 == 2
a = 5 # one '=' means assignment
a == 5 # two '==' is the logical operator for "is equal"
```
## Logical operators and vectors
Logical operators are vectorized. This means that when we ask the question "Is the vector larger than 10?", we get as many `TRUE` and `FALSE` answers as there are elements in the vector.
```{r, fig.cap="R vector logical operation", echo = FALSE}
knitr::include_graphics("assets/r_vector_bigger.svg")
```
```{r}
vec = c(23, 1, 60, 21, 21, 5)
vec > 10
```
If we safe these answers in the a new variable, we can use the result to subset the vector.
So the questions "Is the vector larger than 10?" serves as an important step to do the operation "Give me all the numbers of this vector that are larger than 10!".
```{r}
large = vec > 10
vec[large]
# in short, we combine both steps to
vec[vec > 10]
```
## Overview of common logical operators
Comparative operators:
| | |
|----------|--------------------------|
|`==` | equal |
|`!=` | not equal |
|`<` | less than |
|`<=` | less than or equal |
|`>` | greater than |
|`>=` | greater than or equal |
|`%in%` | is in the set |
There are also functions that test for certain conditions, for example if a value is of a particular class.
Particularly common is the `is.na()` function to check for missing values.
```{r}
is.numeric(5)
is.logical(5)
vec = c(1, 8, 8, NA, 6, NA, 7)
# vectorized
is.na(vec)
# simple check if vector contains any NA value
anyNA(vec)
```
> How can we get rid of the NA values in this vector?
## Linking logical statements
If you want to test multiple statements at once, you'll need the following operators.
| | |
|----------|--------------------------|
|`|` | or |
|`&` | and |
|`!` | not |
To be on the safe side, use parenthesis for the individual logical statements.
```{r}
vec = c(1, 8, 8, 6, 7)
(vec > 5) & (vec < 8)
!anyNA(vec) & max(vec) > 5
```