3.2 Mathematical Operations in R
We will use R for this course, and these operations are all available with R code, allowing R to become a calculator for you. Here are some examples applying the tools above.
3.2.1 PEMDAS
1+2)^3)^2 ((
[1] 729
1 + (2^3)^2
[1] 65
3.2.2 Exponents
You can compare the computation below to match with the rules above. Note how the caret ^ symbol is used for exponents, and the asterisk * is used for multiplication. We also have a function sqrt()
for taking the square root.
## Let's say a = 4 for our purposes
<- 4
a ## And let's say k= 3 and l=5, b=2
<- 3
k <- 5
l <- 2 b
- \(a^0 = 1\)
- \(a^1 = a\)
^0
a^1 a
[1] 1
[1] 4
Note how we use parentheses in R to make it clear that the exponent includes not just k
but (k + l)
- \(a^k * a^l = a^{k + l}\)
^k * a^l
a^(k + l) a
[1] 65536
[1] 65536
Note how we use the asterisk to make it clear we want to multiply k*l
- \((a^k)^l = a^{kl}\)
^k)^l
(a^(k*l) a
[1] 1073741824
[1] 1073741824
- \((\frac{a}{b})^k = (\frac{a^k}{b^k})\)
/ b)^k
(a ^k)/(b^k) (a
[1] 8
[1] 8
- \(a^{-k} = \frac{1}{a^k}\)
^(-k)
a1 / (a^k)
[1] 0.015625
[1] 0.015625
- \(a^{1/2} = \sqrt{a}\)
^(1/2)
asqrt(a)
[1] 2
[1] 2
3.2.3 Summations
Summations and products are a little more nuanced in R, depending on what you want to accomplish. But here is one example.
Let’s take a vector (think a list of numbers) that goes from 1 to 4. We will call it ourlist
.
<- c(1,2,3,4)
ourlist
ourlist## An alternative is to write this: ourlist <- 1:4
[1] 1 2 3 4
Now let’s do \(\sum_{i = 1}^4 (i + 1)^2 = (1 +1)^2 + (1+2)^2 + (1 + 3)^2 + (1 + 4)^2 = 54\)
In R, when you add a number to a vector, it will add that number to each entry in the vector. Example
+ 1 ourlist
[1] 2 3 4 5
We can use that now to do the inside part of the summation. Note: the exponent works the same way, squaring each element of the inside expression.
+ 1)^2 (ourlist
[1] 4 9 16 25
Now, we will embed this expression inside a function in R called sum
standing for summation. It will add together each of the inside components.
sum((ourlist + 1)^2)
[1] 54
If instead we wanted the product, multiplying each element of the inside together, we could use prod()
. We won’t use that function very much in this course.
3.2.4 Logarithms
R also has functions related to logarithms, called log()
and exp()
for the for the natural base \(e\). By default, the natural exponential is the base in the log()
R function.
- \(\log x = 8 \rightarrow e^8 = x\)
exp(8)
[1] 2980.958
log(2980.958)
[1] 8
Note that R also has a number of built-in constants, like pi
.
- \(e^\pi = y \rightarrow \log y = \pi\)
exp(pi)
[1] 23.14069
log(23.14069)
[1] 3.141593
- \(\log (a \times b) = \log a + \log b\)
log(a * b)
log(a) + log(b)
[1] 2.079442
[1] 2.079442
Let’s treat \(n=3\) in this example and enter 3 directly where we see \(n\) below. Alternatively you could store 3 as n, as we did with the other letters above.
- \(\log a^n = n \log a\)
log(a ^ 3)
3 * log(a)
[1] 4.158883
[1] 4.158883
- \(\log \frac{a}{b} = \log a - \log b\)
log(a/b)
log(a) - log(b)
[1] 0.6931472
[1] 0.6931472