Data Types and Structures in R

Cover Vectors, Lists, Data Frames, and Matrices

Learn how to work with R’s core data structures—vectors, lists, data frames, and matrices. This tutorial explains the properties and use cases for each data type and provides practical code examples.

Programming
Author
Affiliation
Published

February 10, 2024

Modified

March 11, 2025

Keywords

R data types, R data structures, R vectors, R lists, R data frames, R matrices

Introduction

R offers a rich set of data structures that serve as the building blocks for data analysis. In this tutorial, we will cover the four primary data types in R:

  • Vectors: The most basic data structure in R.
  • Lists: Versatile collections that can hold elements of different types.
  • Data Frames: Tabular data structures, similar to spreadsheets or SQL tables.
  • Matrices: Two-dimensional arrays used for numerical computations.

Understanding these data structures is essential for writing efficient R code and performing effective data analysis.



Vectors

Vectors are the simplest type of data structure in R. They are ordered, homogeneous collections of elements (all elements must be of the same type).

Creating and Using Vectors

# Creating a numeric vector
num_vector <- c(1, 2, 3, 4, 5)
print(num_vector)                         
[1] 1 2 3 4 5
# Creating a character vector
char_vector <- c("apple", "banana", "cherry")
print(char_vector)                       
[1] "apple"  "banana" "cherry"
# Basic operations on vectors
sum_vector <- sum(num_vector)
print(paste("Sum:", sum_vector))        
[1] "Sum: 15"

When to Use Vectors

Use vectors for storing sequences of numbers, characters, or logical values when all elements are of the same type.

Lists

Lists in R are flexible data structures that can hold elements of different types, including other lists.

Creating and Using Lists

# Creating a list containing different data types
my_list <- list(
  name = "Alice",
  age = 25,
  scores = c(85, 90, 95)
)
print(my_list)
$name
[1] "Alice"

$age
[1] 25

$scores
[1] 85 90 95
# Accessing elements in a list
print(my_list$name)
[1] "Alice"
print(my_list[[3]])
[1] 85 90 95

When to Use Lists

Use lists when you need to store heterogeneous data or a collection of objects that don’t necessarily share the same type.

Data Frames

Data frames are two-dimensional data structures that are ideal for handling tabular data. They are similar to spreadsheets and allow you to store different types of data in each column.

Creating and Using Data Frames

# Creating a data frame
students <- data.frame(
  name = c("Alice", "Bob", "Charlie"),
  age = c(25, 30, 35),
  major = c("Biology", "Mathematics", "Computer Science")
)
print(students)
     name age            major
1   Alice  25          Biology
2     Bob  30      Mathematics
3 Charlie  35 Computer Science
# Accessing columns in a data frame
print(students$name)
[1] "Alice"   "Bob"     "Charlie"

When to Use Data Frames

Data frames are best used when working with structured, tabular data. They are particularly useful in data science for tasks like data cleaning, transformation, and visualization.

Matrices

Matrices are two-dimensional arrays that hold elements of a single data type. They are primarily used for mathematical computations.

Creating and Using Matrices

# Creating a matrix from a vector, specifying the number of rows
matrix_data <- matrix(1:9, nrow = 3, byrow = TRUE)
print(matrix_data)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
# Basic matrix operations
matrix_sum <- sum(matrix_data)
print(paste("Matrix Sum:", matrix_sum))
[1] "Matrix Sum: 45"

When to Use Matrices

Use matrices for numerical computations where you require a fixed two-dimensional structure, such as in linear algebra operations.

Conclusion

Understanding the fundamental data structures in R—vectors, lists, data frames, and matrices—is crucial for effective data analysis. Each structure has its specific use cases, and mastering them will help you write more efficient and readable R code. Experiment with these examples and explore how each data structure can be leveraged in your own projects.

Further Reading

Happy coding, and enjoy exploring R’s powerful data structures!

Back to top

Reuse

Citation

BibTeX citation:
@online{kassambara2024,
  author = {Kassambara, Alboukadel},
  title = {Data {Types} and {Structures} in {R}},
  date = {2024-02-10},
  url = {https://www.datanovia.com/learn/programming/r/basics/data-types-and-structures.html},
  langid = {en}
}
For attribution, please cite this work as:
Kassambara, Alboukadel. 2024. “Data Types and Structures in R.” February 10, 2024. https://www.datanovia.com/learn/programming/r/basics/data-types-and-structures.html.