Chapter 4 Operators
4.1 Mathematical Operators
One thing that R
is really good at doing is mathematical computations. Here’s a few of the basic math operations that you’ll want to get familiar with and be very comfortable comfortable using.
Symbol | Meaning | Example |
---|---|---|
+
|
addition |
3 + 3 = \(1 + 1\)
|
-
|
subtraction |
10 - 1 = \(10 - 1\)
|
*
|
multiplication |
23 * 3 = \(23 \cdot 3\)
|
/
|
division |
276 / 4 = \(\frac{276}{4}\)
|
** or ^
|
exponent |
2 ** 3 = 2 ^ 3 = \(2^3\)
|
exp()
|
\(e\) |
exp(2) = \(\exp \left( 2 \right)\)
|
log()
|
natural log |
log(2.71) = \(\ln{2.71} = e\)
|
log10()
|
log\(_{10}\) |
log10(100) = \(\log_{10} 100\)
|
pi
|
\(\pi\) |
2 * pi = \(2 \cdot \pi\)
|
sqrt()
|
square root |
sqrt(4) = \(\sqrt{4}\)
|
abs()
|
absolute value ( \(|x|\) ) |
abs(-6) = |-6| = 6
|
R
also works in the correct mathematical order of operations: PEMDAS. This stands for
- Parentheses/brackets
- Exponents
- Multiplication
- Division
- Addition
- Subtraction
Because of this, using exra parenthesis is never a bad idea. R
also automatically closes a set of brackets, parenthesis, or braces ({}
) when you open them, but if you’re debugging and deleting make sure that you delete with caution. R
will highlight the corresponding parenthesis when you highlight a different one, but these minor details can become major pains. Coder have caution!
4.2 Logical (Boolean) Operators
As we talked about before, R
can evaluate logical statements in addition to performing math operations. Here’s how this part works:
Symbol | Meaning | Example |
---|---|---|
> | greater than |
3 > 4 \(\implies\) FALSE
|
< | less than |
2 < 9 \(\implies\) TRUE
|
>= | greater than or equal to (\(\geq\)) |
13 <= 13 \(\implies\) TRUE
|
<= | less than or equal to (\(\leq\)) |
14 >= 12 \(\implies\) FALSE
|
== | is exactly equal to |
60 == 61 \(\implies\) FALSE
|
!= | is not equal to (\(\ne\)) |
69 != 818 \(\implies\) TRUE
|
& | logical AND (used in conjunction with one of the above) |
x > 12 & y == TRUE \(\implies\) x is TRUE and y is TRUE
|
| | logical OR (used in conjunction with one of the above) |
x == TRUE | y == TRUE \(\implies\) x is TRUE or y is TRUE
|
4.3 Vectorization
Now it’s time to talk about one of the most useful features of R
: the fact that it’s a vectorized language. This sounds very complex, but what it really means is that you can operate on entire vectors at a time using the operators we just went through. Let’s see a few examples using a vector of length 25. We’ll create a vector v1
that has the numbers 1 through 25, and we’ll do a few operations on it to really see how powerful vectorized operations are.
First, let’s see what simple addition does to the vector. Pretend we want to add 5 to each element?
## [1] 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
## [24] 29 30
What about doubling each element?
## [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46
## [24] 48 50
Cool! But what about that boolean thing we were talking about? Well, let’s try an example. What if we want to find where all the values are greater than 12?
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [12] FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [23] TRUE TRUE TRUE
This is the power of vectorization: you can do things to really large amounts of data in a relatively short amount of time. Obviously, the more complex the operation is, the longer it will take, but it still beats the alternative of having to do the manipulations and calculations by hand.
There’s one last important point to make about vectorization, and that’s what happens when you add vectors of unequal length. We’ve actually already seen examples of this, but we just didn’t realize it. What’s happening is that R
is treating the numbers that we’ve supplied above (5, 2, and 12 respectively) as vectors of length 1, and recycling them over and over in its calculations. If instead we had tried to do v1 + c(5, 10)
, we’d wind up with something different.
## [1] 6 12 8 14 10 16 12 18 14 20 16 22 18 24 20 26 22 28 24 30 26 32 28
## [24] 34 30
This is something that we need to be aware of: even though R
can do a vectorized calculation, we need to make sure that it’s performing the calculation we actually want it to do. Again, the length()
function is your friend here!