Data Types: R

  • Variables can store data of various types.

To check the variable data type, you can use:

  • class()

Name of Data TypeDescriptionCode
NumericRepresents numeric values (integers or decimals).numeric_var <- 42
decimal_var <- 3.14
IntegerRepresents integer values.int_var <- 10L
LogicalRepresents Boolean values (TRUE or FALSE).logical_var <- TRUE
CharacterRepresents text or strings.char_var <- "Hello, World!"
FactorRepresents categorical variables with levels.factor_var <- factor(c("A", "B", "A", "C"))
DateRepresents dates.date_var <- as.Date("2023-01-01")
POSIXctRepresents date and time.datetime_var <- as.POSIXct("2023-01-01 12:34:56", format="%Y-%m-%d %H:%M:%S")
ComplexRepresents complex numbers.complex_var <- 2 + 3i
ListRepresents an ordered collection of objects.list_var <- list(1, "apple", TRUE)
VectorRepresents a one-dimensional array of elements.numeric_vector <- c(1, 2, 3, 4)
MatrixRepresents a two-dimensional array of elements.matrix_var <- matrix(1:6, nrow = 2, ncol = 3)
Data FrameRepresents a tabular data structure with rows and columns.df <- data.frame(Name = c("Alice", "Bob", "Charlie"), Age = c(25, 30, 22), Married = c(FALSE, TRUE, FALSE))
ArraysRepresents multi-dimensional arrays.array_var <- array(1:24, dim = c(2, 3, 4))
EnvironmentRepresents an environment, a collection of symbol-value pairs.env_var <- new.env()<br>env_var$x <- 42

This table summarizes the data types in R along with their descriptions and corresponding code examples.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top