§2024-12-04

R Numbers

¶Numbers in R can be divided into 3 different categories:

In R, the Numeric and Integer types both represent numbers, but they differ in terms of their storage and precision.

  1. Numeric:

    Type: Represents floating-point numbers, i.e., numbers that can have decimals.

    Storage: It uses double-precision (64-bit) to store numbers, which allows it to store both large and small numbers with decimal points.

    Default: By default, when you create a number without specifying it as an integer, R treats it as a Numeric (i.e., double).

    Example:

num1 <- 3.14 # This is a Numeric num2 <- 2 # This is also treated as Numeric (double) by default

You can confirm that a value is numeric using:

is.numeric(num1)   # Returns TRUE
  1. Integer:

    Type: Represents whole numbers (without decimals).

    Storage: Integers are stored as 32-bit signed integers.

    Specific Declaration: To explicitly create an Integer, you need to append an L to the number.

    Example:

int1 <- 5L # This is an Integer

You can confirm that a value is an integer using:

is.integer(int1)  # Returns TRUE

¶Key Differences:

Example:

Numeric example

num <- 10.5 is.numeric(num) # TRUE is.integer(num) # FALSE

Integer example

int <- 10L is.numeric(int) # TRUE (but it's treated as Integer) is.integer(int) # TRUE

¶Performance Consideration: