5 + 3
[1] 8
9 * 2
[1] 18
14 / 3
[1] 4.666667
You must
Performing arithmetic operations is no big deal in R. Simply write any operation using the usual arithmetic operators +
, -
, *
and /
and run your code. No need to type =
.
5 + 3
[1] 8
9 * 2
[1] 18
14 / 3
[1] 4.666667
R allows you to add parentheses (
)
when you need to impose the order of operations. When it comes to raising a number to the power of another one, use the symbol ^
or **
.
(24 + 3) ^ (1 / 3)
[1] 3
In R, calculate:
You can compare 2 elements using the following operators:
>
greater than,>=
greater than or equal to,<
less than,<=
less than or equal to,==
equal to,!=
not equal to.When comparing two elements, R returns either TRUE
or FALSE
.
9 == 3 * 3
[1] TRUE
5 > 4
[1] TRUE
9^2 != 9 * (3 + 6)
[1] FALSE
In R,
pi
More complex operations such as square root, logarithms and exponentiation shall be run using specific functions. These functions are sqrt()
, log()
, exp()
, etc.
In R, calculate
You may need to look at the built-in help (Chapter 7) for some of these.
How to get help (Chapter 7)