-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperators.qmd
198 lines (128 loc) · 4.97 KB
/
operators.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# The primary types of operators in R {#sec-operators}
## Questions
- What are the primary operator types in R?
- How can one use the various operator types in data analysis?
- How do arithmetic, comparison, and logical operators work in R?
- When should I use assignment operators versus comparison operators?
## Learning Objectives
- Identify the primary operator types in R.
- Learn how to use various operators for data manipulation.
- Utilize arithmetic operators for efficient numerical calculations in your R scripts.
- Apply comparison operators to evaluate conditions and filter data based on logical comparisons.
- Employ logical operators to combine multiple conditions and control the flow of your R code.
## Lesson Content
### Introduction
R has many types of operators that can perform different tasks. Here we will focus on 5 major types of operators. The major types of operators are:
- Arithmetic,
- Relational,
- Logical,
- Assignment, and
- Miscellaneous.
### Arithmetic Operators
Arithmetic operators are used to perform mathematical operations. These operators were reviewed in the previous lesson on "Basic arithmetic, arithmetic operators, and variables" (@sec-arithmetic-variables)
### Relational Operators
Relational operators are used to find the relationship between 2 variables and compare objects. The output of these comparisons is Boolean (TRUE or FALSE). The table below describes the most common relational operators.
| Relational Operator | Description |
|:-------------------:|:------------------------:|
| `<` | Less than |
| `>` | Greater than |
| `<=` | Less than or equal to |
| `>=` | Greater than or equal to |
| `==` | Equal to |
| `!=` | Not Equal to |
#### Assign values to variables
```{r}
x <- 227
y <- 639
```
a. Less than
```{r}
x < y
```
b. Greater than
```{r}
x > y
```
c. Less than or equal to
```{r}
x <= 300
```
d. Greater than or equal to
```{r}
y >= 700
```
e. Equal to
```{r}
y == 639
```
f. Not Equal to
```{r}
x != 227
```
### Logical Operators
Logical operators are used to specify multiple conditions between objects. Logical operators work with basic data types such as logical, numeric, and complex data types. These operators generally return `TRUE` or `FALSE` values. Numbers greater that 1 are `TRUE` and 0 equals `FALSE`. The table below describes the most common logical operators.
| Logical Operator | Description |
|:----------------:|:------------------------:|
| `!` | Logical NOT |
| `|` | Element-wise logical OR |
| `&` | Element-wise logical AND |
#### Assign vectors to variables
NOTE: Full discussion in the chapter on "Vectors" (@sec-vectors)
```{r}
vector_1 <- c(0,2)
vector_2 <- c(1,0)
```
a. Logical NOT
```{r}
!vector_1
```
```{r}
!vector_2
```
b. Element-wise Logical OR
```{r}
vector_1 | vector_2
```
c. Element-wise Logical AND
```{r}
vector_1 & vector_2
```
### Assignment Operators
These operators assign values to variables. A more comprehensive review can be obtained in the lesson on "Basic arithmetic, arithmetic operators, and variables" (@sec-arithmetic-variables)
### Miscellaneous Operators
These are helpful operators for working in that can perform various functions. A few common miscellaneous operators are described below.
| Miscellaneous Operator | Description |
|:-------------------:|:-------------------------------------------------:|
| `%*%` | Matrix multiplication (to be discussed in subsequent chapters) |
| `%in%` | Does an element belong to a vector |
| `:` | Generate a sequence |
a. Sequence
```{r}
a <- 1:8
a
```
```{r}
b <- 4:10
b
```
b. Element in a vector
```{r}
a %in% b
```
```{r}
9 %in% b
```
```{r}
9 %in% a
```
## Exercises
- List the basic operator types in R and provide an example for each.
- What operators allow you to subset vectors, lists, and data frames in R?
- Explain how the modulus operator (`%/%`) differs from the regular division operator (`/`).
- What are the membership operators `%in%` and `!%in%` used for in R?
- Assign 10 to x using `<-`. Then compare if `x == 10`.
- Calculate `3^4` using arithmetic operators.
- Compare `3 > 5`. Is the result TRUE or FALSE?
- When would you use the sequence operator (`:`) in R? Give an example.
## Summary
The focus of this chapter has been the five major types of operators that can be used in R/RStudio. The operators allow one to perform numerous tasks including basic arithmetic including basic arithmetic, comparisons, subsetting, matrix multiplication, and vector generation. In the next chapter, we will discuss the available data types in R and how to utilize/handle them.