1 + 100
[1] 101
When we have finished this chapter, we should be able to:
The simplest thing we could do with R is arithmetic operations with numbers. For example:
1 + 100
[1] 101
R printed out the result, with a preceding [1]
.
In the previous calculation the +
sign was used to carry out the addition. Table 3.1 presents a list of arithmetic operators available in R.
Operator | Description |
---|---|
+ | addition |
- | subtraction |
* | multiplication |
/ | division |
^ or ** | exponent |
%/% | integer division |
%% | modulo (remainder) |
Remember when using R as a calculator, the order of operations is the same as we would have learned back in school.
From highest to lowest precedence:
( )
^
or **
*
, /
, %/%, %%+
, -
Therefore:
3 + 5 * 2
[1] 13
When multiple operators have the same precedence, they are evaluated in the order they appear in the mathematical expression from left to right:
3 / 5 * 2 # the division 3/5 is done before the multiplication by 2
[1] 1.2
NOTE: To obtain additional information on how R prioritizes operators, we can access the R help section by using the following command:
Use parentheses to group operations in order to force the order of evaluation if it differs from the default, or to make clear what we intend.
(3 + 5) * 2
[1] 16
This can get unwieldy when not needed, but clarifies our intentions. Remember that others may later read our code.
(3 + (5 * (2 ^ 2))) # hard to read
3 + 5 * 2 ^ 2 # clear, if we remember the rules
3 + 5 * (2 ^ 2) # if we forget some rules, this might help
Remember that the text after each line of code is a comment. Anything that follows after the hash symbol #
is ignored by R when executes code.
Relational (or comparison) operators are used to compare between values. Comparisons in R typically evaluate to TRUE
or FALSE
(which in certain circumstances we can abbreviate to T
and F
). Here is a list of relational operators available in R (Table 3.2).
symbol | read as |
---|---|
< | less than |
> | greater than |
== | equal to |
<= | less than or equal to |
>= | greater than or equal to |
!= | not equal to |
Some simple comparisons with integer numbers follow:
Scientific notation is a special way of expressing numbers that are too big or too small to be conveniently written in decimal form. Generally, it expresses numbers in forms of \(m \times 10^n\) and R uses the e notation1.
1 NOTE: that the e notation has nothing to do with the Euler’s number e=2.718.
By default, R will return in the Console the scientific notation if we type a number less than 0.001. For example:
0.05 # the number is greater than 0.001
[1] 0.05
0.0005 # the number is less than 0.001
[1] 5e-04
There are a few special values that are used in R.
In the real world, missing data may occur when recording medical information (e.g., patients lost to follow-up, incomplete medical records). R uses a special numeric value NA
standing for “Not Available” and represents a missing value. Arithmetic operations using NA
produces NA
:
1 + NA
[1] NA
(3 + 5) / NA
[1] NA
There is also a special number Inf
which represents infinity. Fortunately, R has special numbers for this.
This allows us to represent entities like:
1 / 0
[1] Inf
The Inf
can also be used in arithmetic calculations:
Inf + 1000
[1] Inf
The value NaN (stands for “not a number”) represents an undefined value and it is usually the product of some arithmetic operations. For example:
Inf / Inf
[1] NaN
0 / 0
[1] NaN
-Inf + Inf
[1] NaN
Additionally, there is a null object in R, represented by the symbol NULL. (The symbol NULL always points to the same object.) NULL is often used as an argument in functions to mean that no value was assigned to the argument. Additionally, some functions may return NULL. Note that NULL is not the same as NA, Inf, -Inf, or NaN.