INTRODUCTION TO R
SIMPLE CALCULATIONS
ORDER OF OPERATION
R uses the BEDMAS order of operation:
1) Brackets ()
2) Exponents ^
3) Division / and Multiplication *
4) Addition + and Subtraction -
If we want to force R to calculate, for example, the addition in a command first, we have to enclose it in
brackets
For operations on the same level (e.g. division and multiplication), R will go from left to right
VARIABLES
= a label for a certain piece of information, or even several different pieces of information
- To assign a value to a variable, we use the assignment operator <-, e.g. sales <- 350
• We can also use -> or = (e.g. 350 -> sales), but these are used less
▪ Be mindful of directionality! -> or <- point towards the variable, not the value, and only variable =
value works (not value = variable)
• This does not provide any output, but R will process this internally
• We can, however, check by typing the name of the variable and running this code
- R lets us overwrite, meaning that assigning a new, different value to the previously set variable name will
erase the memory of the first value
- We can do calculations with variables, e.g. sales * royalty will result in the same output as multiplying their
values alone
- Disclaimer: you cannot use spaces in variable names
VECTORS
= a variable that can store multiple values
- To assign multiple values to a variable, we use the combine function c(), e.g. sales.by.month <- c(0, 100,
200, 50, 0, 0, 0, 0, 0, 0, 0, 0)
• This creates a single variable that is a vector containing a number of elements
• The combine function can also be used with vectors, e.g. sales.by.month.extended <- c(sales.by.month,
99)
- To extract information from a vector (e.g. the number of sales in the month of February), we use square
brackets [], e.g. sales.by.month[2] will result in an output of 100
• We can also use the square brackets to alter elements of a vector, e.g. sales.by.month[5] <- 25
• We can add negative indices to remove certain elements, e.g. sales.by.month[-(2:3)] OR
sales.by.month[-c(2,3)] removes elements 2 to 3 in a vector
• We can use the logical operator !=, e.g. salesbymonth[sales.by.month != 0] removes all 0 sales
- We can shorthand a continuous vector by using : , e.g. 2:8 is the same as c(2, 3, 4, 5, 7, 8)
• This can be used both for assigning elements to a vector, altering or extracting information from it
• When replacing information, be aware of the amount of numbers you want replaced and the amount
of numbers in your input, e.g. sales.by.month[1:3] <- c(9, 19) tells R to replace the first three numbers
but only provides two, resulting in an error
• But beware that R does have a recycling rule! E.g. sales.by.month[1:4] <- c(9, 19) WILL work as it uses
“9” as a replacement for both the 1st and 3rd value
, STAT3 – juni 2025 2
- When we multiply/add/subtract/divide/take powers of a vector by a single number, all elements in the
vector get multiplied/added/subtracted/divided/taken powers of
- To quickly find out how many elements a vector has, use the length() function
The recycling rule means that R “recycles” the value of the shorter vector several
times, e.g. when y has a length of 2, as compared to x with a length of 6, then the
third element of x is added to the first element of y again and so on
- This rule also applies for subtraction, multiplication and division
- If the length of the longer vector is not an exact multiple of the length of the
shorter one (e.g. 5 and 2), R will still do it but give us a warning
WARNINGS AND ERRORS
Both errors and warning signal something is off
- When R throws an error, R couldn’t do what we asked it to do, so it stopped, producing no output
- With a warning, it powered through and produced output, but it thinks something could be off, so we
should look at the code and the output with extra care
LOGICAL DATA
A logical value is an assertion about whether something is true or false
- We can test this using the equality operator ==, e.g. 2 + 2 == 4 → TRUE
• This is different from the assignment operator = !
- R will output either TRUE or FALSE
- We can also assign the value directly, e.g. is.it.correct <- TRUE
• We can even shortcut using T and F (but only in capitals)
- We can also both assign logical values in a vector as use the equality operator to extract logical values from
a vector
- It is possible to use logical values in numerical operations, where true will change to a value of 1 and false
to a value of 0
OTHER LOGICAL OPERATORS
In the or operator, at least one of the statements needs to be true for R to return a TRUE-value