B Simple math

It is assumed that students are familiar with basic mathematics and their related symbols or arithmetic operators. However, some symbols may be different. For example the multiplication symbol within R is * rather than x or . when writing it by hand.

Here are a few reminders of mathematical operators and their symbols for arithmetic and logical operations.

The first 3 minutes of this 7 minutes video Arithmetic, Rational, Logical Operators - Introduction to R Programming - Part 456 summarizes the tables below.

B.1 Arithmetic operators

Here is a table defining the arithmetic operators represented by the symbols used within R. These operators are used on numbers or groups of numbers.

Arithmetic operators and their symbols in R
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
^ or ^ Exponentiation
%% Modulo

Depending on the complexity of the calculation it may be necessary to use parenthesis (( ) ) to separate values.

It is important to remember the notion of precedence as detailed below from Wikipedia.57

In mathematics and computer programming, the order of operations (or operator precedence) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given mathematical expression.*

The order is: Parentheses, Exponents, Multiplication, Division, Addition, Subtraction. In the United States, the acronym PEMDAS is common*. (See Wiki reference for other countries.)

Misinterpreting any of the above rules to mean “addition first, subtraction afterward” would incorrectly evaluate the expression

10 − 3 + 2.

The correct value is 9 (not 5, as would be the case if you added the 3 and the 2 before subtracting from the 10).

B.2 Boolean values

A Boolean value is either true or false. These values can be the result of a logical operator (see below) or a statement within an R function, for example stating that there is (T) or there isn’t (F) a header in a table of numbers.

Boolean values
Value Notation
true TRUE or T in uppercase
false FALSE or F in uppercase

B.3 Rational operators

Rational operators
Operator Description
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Exactly equal to
!= Not equal to

B.4 Logical operators

Logical operators can be used to create conditional statements as they result in Boolean values of true or false.

The symbols used imply the boolean operators “AND”, “OR” and “NOT”.

Operator Description
x & y x AND y
x | y x OR y
!x NOT x
isTRUE(x) Test if x has Boolean value TRUE