4.3 Math

A lot of mathematical operations in R are straightforward. Here are some of the basic operations we can perform:

+ and -: addition and subtraction
* and /: multiplication and division
**: exponents

We can perform mathematical operations on values directly:

print(2 + 3)
## [1] 5

Or we can operate on variables:

x <- 5 
print(x**3)
## [1] 125

We can also save the output of an expression as a variable:

my_product <- 2 * 10
print(my_product)
## [1] 20

4.3.1 Order of Operations

R follows the usual mathematical order of operations. And like in math, we can use parentheses () to enforce a specific order.

print(2 * (2 + 2))
## [1] 8