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.
- Addition:
## [1] 12
- Subtraction:
## [1] 5
- Exponentiation:
## [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:
Do ! N O T ! 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 #):
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:
## [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. Vectors are created using the combine
function c()
in R.
## [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:
## [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:
Now we can calculate the Body Mass Index (BMI) using the BMI formula:
## [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.
## [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:
## [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.
## [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:
## [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:
## [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:
Now it’s your time to get into coding: Try Exercise 1.