# Chapter 4 Mathematical Operations

## 4.1 Addition

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\).

`## [1] 7`

`## 2 + 5 = 7`

`## [1] 5`

`## [1] 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!

`## [1] 20`

`## [1] 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\)

`## [1] 625`

`## [1] 625`

`## [1] 25`

`## [1] 25`

`## [1] 5`

`## [1] 5`

`## [1] 1`

`## [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:

`## The square of 10, or 10*10, equals 100`

`## The square root of 100 equals 10`

`## 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\).

`## 2 raised to the power 3 equals 8`

`## The cubic root of 8 equals 2`

`## 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.

**P**arentheses

**E**xponents

**M**ultiplication and**D**ivision

**A**ddition and**S**ubtraction

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:

`## [1] 42`

`## [1] 42`

`## [1] 56`

`## [1] 7`

`## [1] 25`

`## [1] -34`

`## [1] 34`

`## [1] -58`

`## [1] 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:

`## [1] 56`

`## [1] -56`

`## [1] -56`

`## [1] 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!

`## [1] 13`

`## [1] 13`

`## [1] 3`

`## [1] 9`

- Distributive law

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

`## [1] 40`

`## [1] 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.

`## [1] TRUE`

Another rule:

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

`## [1] 5`

`## [1] 5`

`## [1] TRUE`

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

`## [1] 3`

`## [1] 12.5`

`## [1] FALSE`

## 4.8 Rules for Fractions

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

`## [1] TRUE`

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

`## [1] TRUE`

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

`## [1] 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:

`## [1] 64`

`## [1] 64`

`## [1] -512`

`## [1] -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:

`## [1] 64`

Parentheses make all the difference!