1.5 Value types

Some common value types in R are double, integer, logical, character, and datetime. Mostly, you will work with double, logical, and character. You can check the type of the values in an object using an is. function as shown here.

Double

Values of type double are numbers with decimal places.

# By default, R will store a number as double, not integer
x <- 5.23
x
## [1] 5.23
is.integer(x)
## [1] FALSE
is.double(x)
## [1] TRUE

Integer

An integer value is a whole number and requires an L to be added after the number to tell R the number is to be stored as an integer, not double.

x <- 5L  # Add an L to make it an integer
x  # You cannot tell x is an integer just by looking at it
## [1] 5
is.integer(x)
## [1] TRUE
is.double(x)
## [1] FALSE

NOTE: Unless you tell R something is an integer, it will store it as double. Which is just as well since you will mostly be working with doubles, not integers. In general, you can ignore this distinction but occasionally it comes up so it is good to know it exists.

Logical

Logical values are very common in R as we often are comparing things to see if they are the same or not.

x <- c(TRUE, TRUE, FALSE)
x
## [1]  TRUE  TRUE FALSE
is.logical(x)
## [1] TRUE

Character

A character value is a single string surrounded by quotes.

x <- "elevated"
is.character(x)
## [1] TRUE

Datetime

Date and time values can be rather complicated to work with. For more information about working with datetime variables, see the chapter “Dates and Times” in the “R for Data Science” (Wickham and Grolemund 2017).

x <- Sys.time() # Assign the current date and time to x
x
## [1] "2023-09-07 09:50:36 EDT"
class(x) # There is no is. function for this value type, but you can use class
## [1] "POSIXct" "POSIXt"

References

Wickham, Hadley, and Garrett Grolemund. 2017. R for Data Science: Import, Tidy, Transform, Visualize, and Model Data. 1st edition. Sebastopol, CA: O’Reilly Media.