# Storing the integer 24 in the variable my_favorite_integer
my_favorite_integer <- 24L30 Type: Integer and numeric
The machine version of discrete and continuous numbers
The primary function of a program is to perform calculations. The most obvious objects that can be created, stored, and manipulated are numbers. There are two types of numbers, following the concepts in mathematics (Chapter 3) and in statistics (Chapter 17).
A whole number (discrete variables) are represented by integers and are used for counting and indexing. In R programming, real numbers (continuous variables) are called numeric.
While knowing these distinctions is important, R is generally very flexible regarding the two types. It handles conversions automatically without failing or issuing errors if a numeric is used where an integer is expected. For instance, attempting to use the numeric 2.3 instead of the integer 2 to specify the length of a vector (Chapter 35) are handled internally.1
Formal
Assigning an integer value to a variable: to force R to interpret a number as an integer, append
Lafter the number.Assigning a numeric value to a variable: numeric generally refers to numbers with decimals. The decimal separator in programming languages is a period
.. A comma,is used as a list separator (see Chapter 35, Chapter 36, and Chapter 39).# Storing the number 24.022 a_real_number <- 24.022If L is not specified, R automatically assumes that the value is numeric.
my_fake_integer <- 24
Examples
# Built-in constants (such as pi) are natively available in R
pi[1] 3.141593
# Mathematical functions like sqrt() evaluate to numeric values
sqrt(2)[1] 1.414214
# Assign a numeric value to a variable
my_number <- 2.13
# The 'L' suffix forces R to store the value explicitly as an integer
a <- 2L
# Without the 'L' suffix, whole numbers default to numeric
cc <- 2
# str() inspects the internal structure of an object; here it confirms 'a' is an integer
str(a) int 2
# str() confirms 'cc' is a numeric
str(cc) num 2
The type of object is shown on the left, above int for integer and num for numeric.
Tips and warnings
An integer requires less computer memory. However, memory is usually not an issue, and memory management is beyond the scope of this handbook.
As a shortcut, the leading zero before a decimal point can be omitted:
# .123 is the same as 0.123 a <- .123Machine vs. mathematics: there are limits on the number of decimals and the maximum value a computer can store and compute.
# The largest integer .Machine$integer.max[1] 2147483647# The largest postive numeric .Machine$double.xmax[1] 1.797693e+308# The smallest value (machine precision) .Machine$double.eps[1] 2.220446e-16Astronomically large numbers are not supported and are rarely required in typical ecological workflows.
Due to precision limitations, multiplying very small numbers by very large numbers can lead to numerical instability and should be avoided.
Infinity is not a finite number and is represented by
Infand-Inf
The drawback is that one must either trust R to manage the mistake or understand how it handles implicit coercion to use it efficiently.↩︎