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 understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.