3  R as calculator

When we have finished this chapter, we should be able to:

Learning objectives
  • Know the basic arithmetic operators in R.
  • Use R as a calculator.
  • Understand the use of relational (or comparison) operators.
  • Get familiar with scientific notation.
  • Understand special values (i.e. NA, Inf, NaN, NULL) in R

 

3.1 Arithmetic Operators in R

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.

Table 3.1: Basic arithmetic operators 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:

  • Parentheses: ( )
  • Exponents: ^ or **
  • Multiplication, division, integer division, modulo: *, /, %/%, %%
  • Addition, subtraction: +, -

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:

?Syntax

 

3.1.1 Parentheses

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
Important

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.

 

3.2 Relational Operators in R

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).

Table 3.2: Relational (comparison) operators in R
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:

Examples
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

 

3.3 Scientific notation

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.

Examples
  • 0.0055 is written \(5.5 \times 10^{-3}\)
    because 0.0055 = 5.5 × 0.001 = 5.5 × \(10^{-3}\) or 5.5e-3

  • 0.000000015 is written \(1.5 \times 10^{-8}\)
    because 0.000000015 = 1.5 × 0.00000001 = 1.5 × \(10^{-8}\) or 1.5e-8

  • 5500 is written \(5.5 \times 10^{3}\)
    because 5500 = 5.5 × 1000 = 5.5 × \(10^{3}\) or 5.5e3

  • 150000000 is written \(1.5 \times 10^{8}\)
    because 150000000 = 1.5 × 100000000 = 1.5 × \(10^{8}\) or 1.5e8

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

 

3.4 Special values in R

There are a few special values that are used in R.

3.4.1 Missing values (NA)

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

 

3.4.2 Infinitive: -Inf or Inf

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

 

3.4.3 Not A Number (NaN)

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

 

3.4.4 NULL

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.