Math Operators & Vectorizing

Learning Goals

At the end of this lesson, you should:

  • understand sorts of math operators available in R and how to use them
  • understand logical operators in R
  • be aware of the order of operations
  • be aware of how whitespace is interpreted in R
  • understand how to use parentheses, brackets, braces and quotations in R
  • be able to repeat a set of operations across a vector
  • be able to create a sequence of numbers in R using any starting value and any ending value

R as a calculator

Addition, subtraction, multiplication, division

1 + 3
[1] 4
10 - 15
[1] -5
2*8
[1] 16
60/12
[1] 5

A hard return between lines of code is sufficient to separate the commands.

Exponentiation

3^2
[1] 9
2^4
[1] 16
9^0
[1] 1
2^-2
[1] 0.25

R can also handle scientific notation. This number, 3e2 is equivalent to \(3 * 10^2\), or \(3000\).

Roots (square, cube, ….)

4^(1/2)
[1] 2
8^(1/3)
[1] 2

Logs

log(10)
[1] 2.302585

(base e)

log with base 10

log10(10)
[1] 1

log with base 2

log2(4)
[1] 2

If you have other bases:

log(10, base = 4)
[1] 1.660964

Operations with sign

(positive and negative signs are called “unary operators”)

3*-4
[1] -12

Like in standard math, only negatively signed numbers need to be specified.

Integer division (the remainder is discarded)

5 %/% 3
[1] 1

Modulus operator (return the remainder after division)

5 %% 3
[1] 2

….and so much more

Logical Operators

These test for conditions (“is this true?”) and return either a TRUE or FALSE

syntax Function
== equal
!= does not equal
< less than
> greater than
<=, >= less than and equal to, and greater than equivalent

Examples

1 == 1
[1] TRUE
1 == 2
[1] FALSE
1 != 2
[1] TRUE
1 < 1
[1] FALSE
1 > 1
[1] FALSE
1 <= 1
[1] TRUE
1 >= 1
[1] TRUE

When testing multiple conditions: use & (‘and’) if two things must be true and | (‘or’) if one of two things must be true:

1 < 2 & 1 != 1 
[1] FALSE
1 < 2 | 1 != 1
[1] TRUE

Order of operations.

The rules:

  1. operations go left to right
  2. exponents are first, followed by ‘unary operators’ (+/- signs)
  3. multiplication and division before subtraction and/or addition
  4. logical operators come after all mathematical transformations
  5. Parentheses overall all other rules!

What results from this?

2^3+4+12*7/2 <= -6*9

When in doubt about the order of operations use parentheses!

Here is the official R guide to order of operations (warning: this is complicated and refers to functions beyond mathematical operators).

Note

If you become stuck with an unfinished command, you can use the escape key, ESC, to get out of it.

Vectorizing operations

Using R as a calculator between a few numbers is handy, but typically we are hoping to do so much more with it, such as performing a calculations across a long list of numbers.

R is naturally vectorized, which means that you can easily perform a mathematical operation across a vector of numbers (no need to write loops!)

Say we have a collection of numbers from 10 to 20 and we want to multiple them all by 12. We can create a sequence of numbers by wrapping them all in c() command (for “concatenate”) and separating each with a comma.

c(10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
 [1] 10 11 12 13 14 15 16 17 18 19 20

Then those numbers can be operated on by any math operator:

c(10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20) * 10 - 1
 [1]  99 109 119 129 139 149 159 169 179 189 199

There’s also a quicker way to specify a sequence of integers using the notation start:end:

1:10
 [1]  1  2  3  4  5  6  7  8  9 10

It also counts down:

20:10
 [1] 20 19 18 17 16 15 14 13 12 11 10

And works with negative integers:

-5:5
 [1] -5 -4 -3 -2 -1  0  1  2  3  4  5

These can be operated on:

(-5:5)^2
 [1] 25 16  9  4  1  0  1  4  9 16 25
Putting it all together

Check the “History” tab in the upper right hand pane (this should be to the right of the “Environment” tab). What is there?

If you followed along and coded the above examples, you should see the command you ran previously (including any mistakes). This is your command history. There are several icons directly above your history - explore what those do (hoover before clicking any icon to make sure you are okay with action before performing it).