Arithmetic with R
In its most basic form, R can be used as a simple calculator. Consider the following arithmetic operators:
- Addition:
+
- Subtraction:
-
- Multiplication:
*
- Division:
/
- Exponentiation:
^
- Modulo:
%%
The last two might need some explaining:
- The
^
operator raises the number to its left to the power of the number to its right: for example3^2
is 9. - The modulo returns the remainder of the division of the number to the left by the number on its right, for example 5 modulo 3 or
5 %% 3
is 2.
With this knowledge, follow the instructions to complete the exercise.
This is a part of the course
“Introduction to R”
Exercise instructions
- Type
2^5
in the editor to calculate 2 to the power 5. - Type
28 %% 6
to calculate 28 modulo 6. - Submit the answer and have a look at the R output in the console.
- Note how the
#
symbol is used to add comments on the R code.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# An addition
5 + 5
# A subtraction
5 - 5
# A multiplication
3 * 5
# A division
(5 + 5) / 2
# Exponentiation
# Modulo