# Chapter 4 Mathematical Operations

You can use R as a calculator. Type your mathematical expression in the console, and get the result instantaneously.

A better way to use R, is to write your code in R-scripts.

Most often, you will assign values to objects.

Probably the most basic mathematical operation is adding two or more numbers.

Below we assign the value 2 to a, and 5 to b. You can then assign the addition of $$a+b$$ to another object, $$c$$.

2+5
##  7
a = 2; b = 5
c = a + b
cat(a,"+",b,"=",c,"\n")
## 2 + 5 = 7
# if c = a + b, then b = c - a
c-a
##  5
b
##  5

## 4.2 Multiplication and Division

Multiplication is like adding a number several times.

Adding 5 four times, is equivalent to multiplying 5 by 4.

Often, in data science, you will multiply broken numbers. Like $$4.5*3.88$$. The analogy (adding 4.5 3.88 times) is somewhat harder to envision.

The operator for multiplication is the asterisk (*).

Although in mathematical textbooks, you may find $$xy$$ as shorthand for x times y, that doesn’t work in R, and other software. You have to use $$x*y$$ in your code!

5+5+5+5
##  20
4*5
##  20

$$a+a+a+a = 4*a$$ (or 4a, for short)

$$4a = 20$$

We can divide both sides by 4 to find a:

$$4a/4 = 20/4$$ $$\Rightarrow$$ $$a=5$$

## 4.3 Exponentiation

Exponentiation is equivalent to multiplying by the same number, several times. For instance, $$5*5$$ is the same as 5 raised to the power 2, or $$5^{2}$$. Exponentiation in R uses the operator ^, like in the example below.

Exponents do not have to be integers (1, 2, …), but can be broken numbers (e.g. 1.2, 2.8, …).

A special case is an exponent of $$0.5$$. Exponentiation by $$0.5$$ is called the square root.

Taking the square root of a number, is the reverse of taking the square.

If $$x^{2} = y$$, then $$y^{0.5} = x$$. For example, the square of 5 is $$5*5 = 25$$; reversely, the square root of 25 is 5.

Other special cases are exponents of 0 and 1.

$$x^{0} = 1$$

$$x^{1} = x$$

5*5*5*5
##  625
5^4
##  625
5*5
##  25
5^2
##  25
sqrt(25)
##  5
25^(1/2)
##  5
5^0
##  1
5^1
##  5

Exponentiation has the following structure:

$$a^b=c$$

In this formula:

• a is the base
• b is the exponent
• c is the power

## 4.4 Rooting and Logarithms

There is a relationship between exponentiation, rooting and logarithms.

In a simple example, 10 squared (or $$10^{2}$$) is $$10*10 = 100$$.

That is:

$$10^2 = 100$$

Rooting is:

$$\sqrt(100) = 10$$ (the square root of 100)

Logarithm:

$$log(100) = 2$$ (using 10 as the base for the logarithm).

Note that the three numbers (2; 10; and 100) keep coming back, in different settings!

In R this would look like:

cat("The square of 10, or 10*10, equals",10^2,"\n")
## The square of 10, or 10*10, equals 100
cat("The square root of 100 equals",sqrt(100),"\n")
## The square root of 100 equals 10
cat("The logarithm of 100 (base 10) equals", log10(100),"\n")
## The logarithm of 100 (base 10) equals 2

Since 10 is an exceptional base, and squaring and square roots are special cases, we can use a more general version.

Suppose we do the same for $$2^3 = 2*2*2 = 8$$.

cat("2 raised to the power 3 equals",2^3,"\n")
## 2 raised to the power 3 equals 8
cat("The cubic root of 8 equals",8^(1/3),"\n")
## The cubic root of 8 equals 2
cat("The logarithm of 8 (base 2) equals", log(8, 2),"\n")
## The logarithm of 8 (base 2) equals 3

For an easy explanation of the links between roots and exponents, have a look at this video.

## 4.5 The Order of Operations

The order of operations is governed by the principle of PEMDAS.

1. Parentheses
2. Exponents
3. Multiplication and Division

As a general rule, in programming for data science and statistics it is best to use parentheses (brackets) in order to avoid confusion.

Some examples:

2+5*8 
##  42
2+(5*8)
##  42
(2+5)*8
##  56
3+2^2
##  7
(3+2)^2
##  25
12-24-34+12
##  -34
12-(24-34)+12
##  34
12-24-(34+12)
##  -58
12-(24-34+12)
##  10

As you see, formulas are prone to errors!

## 4.6 Negative Numbers in Multiplication

As a rule:

• A positive number times a positive number gives a positive number
• A positive number times a negative number gives a negative number
• A negative number times a negative number gives a positive number

Examples:

8*7
##  56
8*-7
##  -56
-8*7
##  -56
-8*-7
##  56

## 4.7 Rules for Operations

• Commutative law

$$a+b = b+a$$

• Associative law

$$(a+b)+c = a+(b+c)$$

This holds for addition, not for subtraction!

(8+2)+3 
##  13
8+(2+3)  # Same as above
##  13
(8-2)-3 
##  3
8-(2-3)  # Not the same as above!!
##  9
• Distributive law

$$a*(b+c) = (a*b) + (a*c)$$

8*(2+3) 
##  40
8*2 + 8*3
##  40

We can use one command line to evaluate if the latter two expressions are identical.

For these evaluations, you have to use the $$a == b$$ format (double =), which evaluates whether $$a$$ and $$b$$ are the same. $$a = b$$ (single =) would allocate the value of $$b$$ to $$a$$, which is not what we want.

8*(2+3) == 8*2 + 8*3 # Evaluate if the expressions are identical
##  TRUE

Another rule:

$$(b+c) / a = (b/a)+(c/a)$$

(8+7)/3
##  5
(8/3) + (7/3)
##  5
(8+7)/3 == (8/3) + (7/3)
##  TRUE

But: $$(a) / (b+c) \neq (a/b) + (a/c)$$

15/(2+3)
##  3
15/2 + 15/3
##  12.5
15/(2+3) == 15/2 + 15/3
##  FALSE

## 4.8 Rules for Fractions

Rule 1: $$(a/p) + (b/p) = (a+b)/p$$

8/4 + 3/4 == (8 + 3)/4
##  TRUE

Rule 2: $$(a/p)*(b/q) = (a*b)/(p*q)$$

(8/4)*(6/3) == (8*6)/(4*3)
##  TRUE

Rule 3: $$(a/p)/(b/q) = (a/b)*(q/b)$$

(8/4)/(6/3) == (8/4)*(3/6)
##  TRUE

## 4.9 Rules for Exponentiation

Rule 1: $$a^n = a * a * a * ...$$ (n times)

Rule 2: $$a^n$$ is positive if $$a>0$$

Rule 3: $$a^n$$ is positive if $$a<0$$ and n is an even number

Rule 4: $$a^n$$ is negative if $$a<0$$ and n is an odd number

Examples:

8^2    # Rule 2
##  64
(-8)^2 # Rule 3
##  64
-8^3   # Rule 4
##  -512
-8^2   # Is the outcome what you expected??
##  -64

In the expression $$-8^2$$, the PEMDAS rule forces R to first evaluate $$8^2$$ (E, for exponentiation), before multiplying (M) by -1!!

If you intend to square -8 ($$-8*-8=64$$), then the code should read:

(-8)^2   # Is the outcome what you expected??
##  64

Parentheses make all the difference!