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+ba+b to another object, cc.

## [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.53.884.53.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 xyxy as shorthand for x times y, that doesn’t work in R, and other software. You have to use xyxy in your code!

## [1] 20
## [1] 20

a+a+a+a=4aa+a+a+a=4a (or 4a, for short)

4a=204a=20

We can divide both sides by 4 to find a:

4a/4=20/44a/4=20/4 a=5a=5

4.3 Exponentiation

Exponentiation is equivalent to multiplying by the same number, several times. For instance, 5555 is the same as 5 raised to the power 2, or 5252. 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.50.5. Exponentiation by 0.50.5 is called the square root.

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

If x2=yx2=y, then y0.5=xy0.5=x. For example, the square of 5 is 55=2555=25; reversely, the square root of 25 is 5.

Other special cases are exponents of 0 and 1.

x0=1x0=1

x1=xx1=x

## [1] 625
## [1] 625
## [1] 25
## [1] 25
## [1] 5
## [1] 5
## [1] 1
## [1] 5

Exponentiation has the following structure:

ab=cab=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 102102) is 1010=1001010=100.

That is:

102=100102=100

Rooting is:

(100)=10(100)=10 (the square root of 100)

Logarithm:

log(100)=2log(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 23=222=823=222=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.

  1. Parentheses
  2. Exponents
  3. Multiplication and Division
  4. Addition and Subtraction

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+aa+b=b+a

  • Associative law

(a+b)+c=a+(b+c)(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)=(ab)+(ac)a(b+c)=(ab)+(ac)

## [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==ba==b format (double =), which evaluates whether aa and bb are the same. a=ba=b (single =) would allocate the value of bb to aa, which is not what we want.

## [1] TRUE

Another rule:

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

## [1] 5
## [1] 5
## [1] TRUE

But: (a)/(b+c)(a/b)+(a/c)(a)/(b+c)(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(a/p)+(b/p)=(a+b)/p

## [1] TRUE

Rule 2: (a/p)(b/q)=(ab)/(pq)

## [1] TRUE

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

## [1] TRUE

4.9 Rules for Exponentiation

Rule 1: an=aaa... (n times)

Rule 2: an is positive if a>0

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

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

Examples:

## [1] 64
## [1] 64
## [1] -512
## [1] -64

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

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

## [1] 64

Parentheses make all the difference!