3 Tutorial: Using R as a calculator

After working through Tutorial 3, you’ll…

  • be able to work with mathematical operators in R
  • be able to use mathematical operators on variables and 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

3.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

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

However, we still see the BMI of all the other five people as well. How can we chose 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

3.3 Take-Aways

  • Mathematical operators: use +, -, *, /, ^
  • Use case: use these operators on numbers, variables, and vectors

Now that we can use R as a calculator and have been introduced to the concept of variables and vectors, let’s delve into the concept of objects an structures in R: Tutorial: Objects & structures in R