Skip to content

R - Data Types

In R, everything is an object. Variables are not declared with a specific data type; instead, they take on the type of the object assigned to them. Understanding the fundamental data types and the data structures that hold them is key to working effectively in R.

R has two main kinds of data objects: atomic vectors and lists. All other data structures, like matrices and data frames, are built on top of these.

An atomic vector is the simplest R data structure and contains elements of the same type. There are six primary atomic types:

Data TypeDescription & ExampleCheck with class()
logicalBoolean values. TRUE, FALSE.v <- TRUE
print(class(v))
# [1] “logical”
numericReal numbers (floating-point). 12.3, 5, -99.v <- 23.5
print(class(v))
# [1] “numeric”
integerInteger numbers. Use an L suffix. 2L, 34L.v <- 2L
print(class(v))
# [1] “integer”
characterText strings. "hello", 'R'.v <- “R is fun”
print(class(v))
# [1] “character”
complexComplex numbers with an imaginary part. 3 + 2i.v <- 2+5i
print(class(v))
# [1] “complex”
rawHolds raw bytes. Rarely used in data analysis.v <- charToRaw(“Hi”)
print(class(v))
# [1] “raw”

These structures are used to organize and store the atomic types.

A vector is a one-dimensional sequence of data elements of the same type. Use the c() function (for combine) to create a vector with multiple elements.

# Create a character vector
fruit_colors <- c('red', 'green', 'yellow')
print(fruit_colors)
# Get the class of the vector
print(class(fruit_colors))
[1] "red" "green" "yellow"
[1] "character"

A list is a generic vector that can contain elements of different types, including other lists, functions, or data frames.

# Create a list containing a numeric vector, a string, and a function
my_list <- list(numbers = c(2, 5, 3), name = "My List", summary_func = summary)
# Print the list
print(my_list)
$numbers
[1] 2 5 3
$name
[1] "My List"
$summary_func
function (object, ..., maxsum = 7, digits = max(3L, getOption("digits") - 3L),
...) .Primitive("summary")

A matrix is a two-dimensional rectangular data set. Like a vector, all its elements must be of the same type.

# Create a 2x3 matrix from a vector
M <- matrix(c(1:6), nrow = 2, ncol = 3)
print(M)
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6

A factor is a special type of vector for storing categorical data. It stores the data as a vector of integer codes along with a corresponding set of character labels (levels). Factors are useful for statistical modeling and some plotting functions.

# Create a vector of educational levels
education_levels <- c('High School', 'Bachelor', 'Bachelor', 'Master', 'PhD', 'Master')
# Create a factor object
factor_education <- factor(education_levels)
# Print the factor and its levels
print(factor_education)
print(levels(factor_education))
[1] High School Bachelor Bachelor Master PhD Master
Levels: Bachelor High School Master PhD
[1] "Bachelor" "High School" "Master" "PhD"

Data frames are the most common way to store tabular data in R. They are essentially a list of vectors of equal length, meaning each column can have a different data type. The tibble is a modern reimagining of the data frame, offered by the tidyverse. Tibbles are better for modern data science because they are stricter and have more convenient printing methods.

# Load the tidyverse package (or just tibble)
# install.packages("tidyverse")
library(tidyverse)
# Create a tibble (a modern data frame)
patient_data <- tibble(
gender = c("Male", "Male", "Female"),
height_cm = c(152, 171.5, 165),
weight_kg = c(81, 93, 78),
age = c(42L, 38L, 26L) # Use L for integers
)
print(patient_data)

Notice how the tibble neatly prints the data types of each column:

# A tibble: 3 × 4
gender height_cm weight_kg age
<chr> <dbl> <dbl> <int>
1 Male 152 81 42
2 Male 171. 93 38
3 Female 165 78 26