Arithmetic operation in R
- Type
2^5in the editor to calculate 2 to the power 5. - Type
28 %% 6to calculate 28 modulo 6. - Note how the
#symbol is used to add comments on the R code.
2^5 in the editor to calculate 2 to the power 5.28 %% 6 to calculate 28 modulo 6.# symbol is used to add comments on the R code.# An addition
5 + 5
# A subtraction
5 - 5
# A multiplication
3 * 5
# A division
(5 + 5) / 2
# A Exponentiation
2^5
# a Modulo
5 %% 3
# Exponentiation
# Modulo
> # An addition
> 5 + 5
[1] 10
>
> # A subtraction
> 5 - 5
[1] 0
>
> # A multiplication
> 3 * 5
[1] 15
>
> # A division
> (5 + 5) / 2
[1] 5
>
> # A Exponentiation
> 2^5
[1] 32
>
> # a Modulo
> 5 %% 3
[1] 2
>
> # Exponentiation
>
>
> # Modulo
> # An addition
> 5 + 5
[1] 10
>
> # A subtraction
> 5 - 5
[1] 0
>
> # A multiplication
> 3 * 5
[1] 15
>
> # A division
> (5 + 5) / 2
[1] 5
>
> # A Exponentiation
> 2^5
[1] 32
>
> # a Modulo
> 5 %% 3
[1] 2
In its most basic form, R can be used as a simple calculator. Consider the following arithmetic operators:
+-*/^%%The last two might need some explaining:
^ operator raises the number to its left to the power of the number to its right: for example 3^2 is 9.5 %% 3 is 2.With this knowledge, follow the instructions below to complete the exercise.
First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.