Basic Math Tutorial

R enables you to do basic math, using all the usual operators: Addition

2 + 2
## [1] 4
1 + 2 + 3 + 4 + 5
## [1] 15

Subtraction

10 - 1
## [1] 9
5 - 6
## [1] -1

Multiplication

2 * 2
## [1] 4
1 * 2 * 3
## [1] 6

Division

4/2
## [1] 2
10/2/4
## [1] 1.25

Parentheses can be used to adjust order of operations:

10/2 + 2
## [1] 7
10/(2 + 2)
## [1] 2.5
(10/2) + 2
## [1] 7

Check your intuition by checking order of operations on your own functions.

Exponents and square roots involve intuitive syntax:

2^2
## [1] 4
3^4
## [1] 81
1^0
## [1] 1
sqrt(4)
## [1] 2

So do logarithms:

log(0)
## [1] -Inf
log(1)
## [1] 0

and logarithms to other bases, including arbitrary ones:

log10(1)
## [1] 0
log10(10)
## [1] 1
log2(1)
## [1] 0
log2(2)
## [1] 1
logb(1, base = 5)
## [1] 0
logb(5, base = 5)
## [1] 1

natural number exponents use a similar syntax:

exp(0)
## [1] 1
exp(1)
## [1] 2.718

There are also tons of other mathematical operations, like: Absolute value

abs(-10)
## [1] 10

Factorials:

factorial(10)
## [1] 3628800

and Choose:

choose(4, 1)
## [1] 4
choose(6, 3)
## [1] 20