1 + 100 [1] 101
R is a powerful tool for statistical computing and data analysis, but it can also serve as a basic calculator. In this chapter, we explore various arithmetic operations, simple comparisons with numbers, and the use of scientific notation (i.e., e-notation in R) for very small or large numbers. We also discuss how special values such as NA, NaN, and Inf behave in calculations.
One of the simplest tasks in R is performing basic arithmetic operations with numbers. For example:
1 + 100 [1] 101
Here, the + operator performs addition. The result is displayed with a preceding [1], indicating the index of the output. TABLE 3.1 provides a list of the arithmetic operators available in R.
When using R as a calculator, it’s important to note that the order of operations follows the same principles of arithmetic taught in high school mathematics, listed from highest to lowest precedence:
( )
^ or **
*, /, %/%, %%+, -
For example:
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, as shown below:
3 / 5 * 2 # the division 3/5 is done before the multiplication by 2[1] 1.2
INFO
For more detailed information on how R prioritizes operators, we can refer to the R help section using the following command:
?Syntax
Parentheses
Parentheses are used to group operations, either to enforce a specific order of evaluation that differs from the default or to clarify the intended calculation.
(3 + 5) * 2[1] 16
Although it may sometimes seem redundant, using parentheses can clarify our intentions, which is particularly helpful when others review our code later.
(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 helpComparison (or relational) operators are used to compare values. In R, comparisons return TRUE or FALSE, which can be abbreviated as T and F (though this is not recommended). Below is a list of comparison operators available in R (TABLE 3.2).
Some simple comparisons with integer numbers follow:
2 < 1 # less than[1] FALSE
1 > 0 # greater than[1] TRUE
1 == 1 # equal to (double equal sign for equality)[1] TRUE
1 <= 1 # less than or equal to[1] TRUE
-9 >= -3 # greater than or equal to[1] FALSE
1 != 2 # not equal to (inequality)[1] TRUE
Scientific notation is a method of expressing numbers that are very small or very large in a more compact and readable form. It typically follows the format
Example (small number): 0.000000015
# type in Console and press Enter
0.000000015[1] 1.5e-08
Example (large number): 150000000
# type in Console and press Enter
150000000[1] 1.5e+08
To globally disable e-notation in R, we can set options(scipen = 999) at the beginning of our script. To restore the default behavior, simply reset scipen to 0, which will allow R to use e-notation when appropriate.
CAUTION!
The e-notation in this context is not related to Euler’s number (
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). In R, the special numeric value NA stands for “Not Available” and represents a missing value. Arithmetic and comparison operations involving NA will return NA:
1 + NA[1] NA
(3 + 5) / NA[1] NA
3 > NA[1] NA
In R, there is a special value for infinity, denoted as Inf. This allows the representation of expressions such as:
1 / 0[1] Inf
The value Inf can also be used in arithmetic operations, for example:
Inf + 1000[1] Inf
The value NaN (stands for “Not a Number”) represents an undefined or invalid value, typically resulting from certain arithmetic operations. For example:
Inf / Inf[1] NaN
0 / 0[1] NaN
-Inf + Inf[1] NaN
In R, NULL is distinct from both NA and NaN. It represents the null value and is typically used in functions to indicate that an optional argument has not been assigned a value or that a function does not produce a meaningful result (see Chapter ?sec-XX for more on functions).