What are vectors, lists, matrices, and data frames in R?
Updated Feb 20, 2026
Short answer
In R, vectors, lists, matrices, and data frames are fundamental data structures used to store and organize data. A vector stores elements of the same basic type, a list can store elements of different types, a matrix stores data in a two-dimensional structure with one data type, and a data frame stores tabular data where each column can have a different type. These structures form the foundation of data manipulation and analysis in R.
Deep explanation
R provides several built-in data structures for organizing data. The main differences between them are how they store values, whether they allow mixed data types, and how they are accessed.
Vectors
A vector is the simplest and most commonly used data structure in R. It is a one-dimensional collection of elements where all elements must be of the same basic type.
Common vector types include:
- Numeric vectors: store numbers.
- Character vectors: store text.
- Logical vectors: store
TRUEorFALSE. - Integer vectors: store whole numbers.
Example:
numbers <- c(10, 20, 30, 40)names <- c("Alice", "Bob", "Charlie")valid <- c(TRUE, FALSE, TRUE)
numbersOutput:
[1] 10 20 30 40Vectors are created using the c() (combine) function.
Important characteristics:
- All elements must have a compatible type.
- R automatically converts values to a common type when necessary.
Example:
x <- c(1, 2, "three")xOutput:
[1] "1" "2" "three"Because a vector cannot contain both numbers and characters, R converts the numeric values into characters.
Vectors support vectorized operations, meaning operations can be applied to all elements at once:
prices <- c(100, 200, 300)
prices * 1.1Output:
[1] 110 220 330This ability makes R efficient for statistical and mathematical operations.
---
Lists
A list is a more flexible data structure that can store elements of different types, including other lists.
Unlike vectors, lists do not require all elements to have the same data type.
Example:
person <- list( name = "Alice", age = 25, scores = c(90, 85, 95))
personA list can contain:
- Numbers
- Strings
- Vectors
- Matrices
- Data frames
- Other lists
- Functions
Lists are useful when storing related but different types of information.
Accessing list elements:
person$nameOutput:
[1] "Alice"Using double brackets accesses the actual object stored inside:
person[[3]]Output:
[1] 90 85 95Lists are commonly used because many advanced R objects, such as statistical model results, are internally stored as lists.
---
Matrices
A matrix is a two-dimensional data structure containing rows and columns. Every element in a matrix must have the same data type.
A matrix can be created using the matrix() function.
Example:
m <- matrix( 1:6, nrow = 2, ncol = 3)
mOutput:
[,1] [,2] [,3][1,] 1 3 5[2,] 2 4 6Matrices are useful for mathematical operations, especially in statistics, linear algebra, and machine learning.
Accessing matrix elements:
m[1, 2]Output:
3The first value represents the row and the second represents the column.
Example:
m[2, ]returns the second row, while:
m[, 3]returns the third column.
Important characteristics:
- Two-dimensional structure.
- All values must have the same type.
- Supports mathematical operations efficiently.
---
Data Frames
A data frame is one of the most important structures in R for data analysis. It represents data in a table format with rows and columns.
Each column is stored as a vector, so different columns can contain different data types.
Example:
employees <- data.frame( name = c("Alice", "Bob", "Charlie"), age = c(25, 30, 35), salary = c(50000, 60000, 70000))
employeesOutput:
name age salary1 Alice 25 500002 Bob 30 600003 Charlie 35 70000A data frame can contain:
- Character columns
- Numeric columns
- Logical columns
- Factor columns
- Date columns
Accessing data frame columns:
employees$nameor:
employees[["salary"]]Accessing rows:
employees[1, ]returns the first row.
Data frames are commonly used for:
- Reading CSV files.
- Cleaning datasets.
- Statistical analysis.
- Machine learning workflows.
---
Comparison of R Data Structures
| Structure | Dimensions | Mixed Data Types Allowed? | Example Use |
|---|---|---|---|
| Vector | 1D | No | A list of temperatures |
| List | 1D | Yes | Storing a model result with different objects |
| Matrix | 2D | No | Mathematical calculations |
| Data frame | 2D | Yes (by column) | Spreadsheet-like datasets |
The relationship between these structures is important:
- A vector is the basic building block of many R objects.
- A list provides flexibility by allowing different objects together.
- A matrix extends vectors into two dimensions.
- A data frame combines multiple vectors as columns into a table.
Real-world example
Suppose a data analyst is working with employee information.
A vector might store employee ages:
ages <- c(25, 30, 35, 40)A list might store information about one employee:
employee <- list( name = "Alice", age = 25, skills = c("R", "SQL", "Python"))A matrix might store monthly sales numbers:
sales <- matrix( c(100, 120, 150, 200), nrow = 2)
salesA data frame might store the complete employee dataset:
employees <- data.frame( name = c("Alice", "Bob"), age = c(25, 30), department = c("Analytics", "Finance"))
employeesIn practice, analysts usually load raw datasets into data frames, perform transformations on columns (vectors), and may use matrices for numerical computations inside statistical algorithms.
Common mistakes
- * Assuming vectors in R can contain unrelated data types without conversion.
- * Confusing a list with a vector because both can store multiple values.
- * Thinking matrices can contain different data types in different columns.
- * Treating data frames as simple matrices and forgetting that each column can have its own type.
- * Using single brackets and double brackets interchangeably when accessing lists.
- * Forgetting that data frames are made up of vectors stored as columns.
- * Creating a matrix when a data frame is more appropriate for tabular business data.
Follow-up questions
- What is the difference between a vector and a list in R?
- Why are data frames commonly used in data analysis?
- How do you access elements in a data frame?
- What happens when different data types are combined in a vector?
- What is the difference between a matrix and a data frame?
- Are data frames stored as lists internally in R?