01 / 20

Converting
Data Structures in R

Learn to seamlessly convert between vectors, lists, and data frames: the three foundational building blocks of R programming.

R Programming

What Are Data Structures?

Data structures are containers that organize and store your data in R. Each structure has its own rules about what kind of data it can hold and how you can access it. The three most important ones for beginners are:

Vector
A flat sequence of values, all the same type (e.g., all numbers or all text).
List
A collection that can hold mixed types and even other lists or data frames.
Data Frame
A table of rows and columns, where each column is a vector of equal length.

The Big Picture: How They Relate

Vector Same type, flat List Mixed types, nested Data Frame Table of vectors as.list() unlist() as.data.frame() as.list() data.frame() wraps vectors as columns Conversion Functions Connect All Three df$col or unlist(df) extracts vectors

Why Convert Between Structures?

Different R functions expect different input types. If your data is in the wrong container, you will get errors or unexpected results. Converting lets you:

1
Feed functions the right type: Many statistical functions require vectors, not lists or data frames.
2
Reshape imported data: CSV files arrive as data frames; you may need vectors for calculations.
3
Combine heterogeneous results: Functions like lapply() return lists that you often want as vectors or data frames.

The Core Conversion Functions

R Vector List Data Frame as.list() unlist() as.data.frame() as.list() data.frame() df$col

The Golden Rule of Conversion

logical integer double character R Type Coercion Hierarchy strictest most flexible

Vector vs List: Key Differences

Vector List
Data types Homogeneous (one type) Heterogeneous (mixed)
Access element x[1] x[[1]] or x$name
Nesting Not possible Lists inside lists
Created with c() list()
Convert to other as.list() unlist()

How to Choose a Conversion

1
Identify the source: Use class(x) or str(x) to check what you have.
2
Identify the target: What does the function you need expect? Check its documentation.
3
Pick the right function: Use the conversion map: unlist(), as.list(), as.data.frame(), etc.
4
Watch for coercion: If mixing types, check whether R silently changed your data types.
5
Verify the result: Run str(result) to confirm the output structure is correct.

Example 1: Vector → List

# Start with a numeric vector scores <- c(85, 92, 78, 95) class(scores) # "numeric" # Convert to a list score_list <- as.list(scores) class(score_list) # "list" # Each element is now a separate list item score_list[[2]] # 92

Example 2: List → Vector

# A list of numbers temps <- list(72, 68, 75, 80) # Flatten into a vector temp_vec <- unlist(temps) class(temp_vec) # "numeric" # Now you can do math! mean(temp_vec) # 73.75

Example 3: Vectors → Data Frame

# Two vectors of equal length names <- c("Alice", "Bob", "Carol") grades <- c(90, 85, 92) # Combine into a data frame df <- data.frame( student = names, score = grades ) # Result: # student score # 1 Alice 90 # 2 Bob 85 # 3 Carol 92

Example 4: Data Frame → Vector & List

# Extract a column as a vector score_vec <- df$score class(score_vec) # "numeric" # Convert entire data frame to a list df_as_list <- as.list(df) str(df_as_list) # List of 2 # $ student: chr "Alice" "Bob" "Carol" # $ score : num 90 85 92

Example 5: Named List → Data Frame

# A named list with equal-length vectors my_list <- list( city = c("NYC", "LA", "CHI"), pop = c(8.3, 3.9, 2.7) ) # Convert to data frame city_df <- as.data.frame(my_list) # city pop # 1 NYC 8.3 # 2 LA 3.9 # 3 CHI 2.7

Common Mistakes to Avoid

✕ Unequal lengths
Using data.frame() with vectors of different lengths causes an error or silent recycling.
✕ Silent coercion
Running unlist() on a mixed list turns numbers into characters without warning.
✕ Using [ ] instead of [[ ]]
Single brackets on a list return a sub-list, not the element itself. Use double brackets [[ ]] to extract values.

Complete Conversion Reference

From → To Function Watch For
Vector → List as.list(x) Each element becomes a list item
List → Vector unlist(x) Type coercion with mixed types
Vectors → Data Frame data.frame(a, b) Vectors must be equal length
List → Data Frame as.data.frame(x) List elements must be equal length
Data Frame → List as.list(df) Returns named list of column vectors
Data Frame → Vector df$col Extracts one column only

Question 1 of 5

What does unlist(list(10, 20, 30)) return?

Question 2 of 5

What happens when you run unlist(list(1, "hello", TRUE))?

Question 3 of 5

Which function converts a data frame into a list of its columns?

Question 4 of 5

You have two vectors: x <- c("A", "B") and y <- c(1, 2, 3). What happens with data.frame(x, y)?

Question 5 of 5

Given my_list <- list(a = 1:3, b = 4:6), what is the class of as.data.frame(my_list)$a?