Section 3 Using R as a calculator
To do addition, subtraction, multiplication and division, use the symbols +
, -
, *
, /
respectively. For example, if you type 5 * 6
at the command prompt, and press return, you will see
5 * 6
## [1] 30
The symbol ^
is used for raising a number to a power. For example, to calculate \(2^4\), try the following
2 ^ 4
## [1] 16
3.1 Scientific notation
Large numbers may be displayed using “scientific notation”. For example, if we calculate \(500^3\) in R, instead of displaying 125000000
as the result, we see
500 ^ 3
## [1] 1.25e+08
When you see e
appear within a number, read it as “multiplied by 10 to the power of”. So 1.25e+08
is read as “1.25 multiplied by 10 to the power of 8”.
Exercise 3.1 Replace x
with a suitable number in the following command
1 / 1000 ^ x
to produce the output
## [1] 1e-09
Check your answer by trying the calculation in R.
3.2 Order of operations
R will follow the BODMAS (Brackets, Orders (powers/roots), Division, Multiplication, Addition, Subtraction) rule for the order in which it will carry out calculations.
Exercise 3.2 First, predict what result you would get from each of these commands. Then try them in R.
4 / 2*2
4 / (2*2)
16 ^ 1/2
16 ^ (1/2)