2 Tutorial: Using R as a calculator

After working through Tutorial 2, you’ll…

  • be able to work with mathematical operators in R
  • be able to use mathematical operators on variables and vectors
  • subset values from vectors

One of the first things everyone learns in R is to use R as a calculator. You have access to many mathematical operators in R (e.g. +, -, *, /, ^). Let’s try some of them.

  1. Addition:
5+7
## [1] 12
  1. Subtraction:
12-7
## [1] 5
  1. Exponentiation:
3^3
## [1] 27

2.1 Using variables for calculation

You can also assign numbers to variables with the assign operator “<-”. We have already talked about assigning word or numbers to variables in the chapter Writing Code. Please remember that a variable name in R can include numeric and alphabets along with special characters like dot (.) and underline (_).’

Therefore, these are good options to name your variables:

my_1st_number <- 3
my.1st.numer <- 3

Do !not! use these variable names because they will cause errors and throw warning messages. I have therefore put the code as annotation to avoid the warning messages (with #):

#  _number <- 3
#  .number <- 3
#  my-1st-number <- 3

You can use variables in your calculations by assigning the numbers to variables (i.e. store the numerical value in the variable).’

five <- 5 
seven <- 7
twelve <- five + seven # here you add the two variables in which the numbers are stored. The result of the addition is stored in the variable "twelve"
twelve                 # now you have to retrieve the content of the variable, so that the result is printed to the console
## [1] 12

The names of the variables are freely selectable. For example, you can also proceed like this:

three <- 5
three  # print the content of the variable to the console
## [1] 5

2.2 Using vectors for calculation

You can also store more than one number in a variable. We call this process “creating vectors” because variables that contain more than one number are called “vectors” in R (we’ll get to vectors in [Tutorial: Objects & structures in R]). Vectors are created using the combine function c() in R.

twelve <- c(1,2,3,4,5,6,7,8,9,10,11,12) 
twelve # print the content of the variable to the console
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12

Again, the variable name is chosen arbitrarily. You can also do this:

twelve <- c(4,10,15,21,33)
twelve # print the content of the variable to the console
## [1]  4 10 15 21 33

You can use mathematical operations on vectors (e.g., +, -, * and /). Let’s create two vectors “weight” and “height” that contain the weight and height measures of 6 individuals. For example, the first individual weighs 60 kg and is 1.75 m tall:

weight <- c(60, 72, 57, 90, 95, 72)
height <- c(1.75, 1.80, 1.65, 1.90, 1.74, 1.91)

Now we can calculate the Body Mass Index (BMI) using the BMI formula:

BMI <- weight/height^2
BMI # print the content of the BMI variable to the console
## [1] 19.59184 22.22222 20.93664 24.93075 31.37799 19.73630

Now we know that the first person has a BMI of 19.59, which is within the range of normality (18.5 and 24.9).

2.3 Selecting values from a vector

We still see the BMI of all the other five people, i.e. the entire vector. How can we select only the first person?

You can select values from a vector by using square brackets [ ] and enter the number of the entry that you want to print to your console.

BMI[1]
## [1] 19.59184

Again, you can see that the first person has a BMI of 19.59. You could also decide to look at all values except the first one:

BMI[-1]
## [1] 22.22222 20.93664 24.93075 31.37799 19.73630

You can even use the [ ] selector on vectors that consist of words instead of numbers. These vectors are called “character vectors”, while vectors that contain numbers are called “numeric vectors”. Let’s create a character vector that contains the BMIs of the six individuals as words. We’ll need to put quotation marks around your entries so that R knows that those values are words not numbers.

BMI_word <- c("nineteen", "twenty-two", "twenty", "twenty-four", "thirty-one", "nineteen")
BMI_word
## [1] "nineteen"    "twenty-two"  "twenty"      "twenty-four" "thirty-one" 
## [6] "nineteen"

We’ll now select only the first value of of this BMI_word character vector:

BMI_word[1]
## [1] "nineteen"

If you want to select multiple values, you can index them. Let’s select the BMI of the third, fourth and fifth individual:

BMI_word[3:5]
## [1] "twenty"      "twenty-four" "thirty-one"

2.4 Take-Aways

  • Mathematical operators: use +, -, *, /, ^
  • Use case: use these operators on numbers, variables, and vectors
  • Create vectors: use the combine function c()
  • Select values from vectors: use square brackets [ ]

2.5 Additional tutorials

You still have questions? The following online guides can help you with that: