rguides

How to Convert Lists to Data Frames in R

Lists often result from reading JSON data, scraping websites, or collecting API responses in R. When you convert lists to data frames, the data becomes usable for analysis and plotting. The approach depends on whether your list elements have equal lengths and whether you need nested structure handling.

# Named list with equal-length elements
my_list <- list(
  name  = c("Alice", "Bob", "Charlie"),
  age   = c(25, 30, 35),
  score = c(85, 90, 78)
)

df <- as.data.frame(my_list)
#     name age score
# 1  Alice  25    85
# 2    Bob  30    90
# 3 Charlie 35    78

For lists with unequal-length elements, dplyr::bind_rows() pads shorter vectors with NA. data.table::rbindlist() is the fastest option for large datasets. For a list of data frames (not named vectors), bind_rows() stacks them row-wise and can add an .id column to track the source element. purrr::list_rbind() (purrr >= 1.0) is the modern row-binding function that handles mismatched columns predictably.

library(dplyr)

# bind_rows handles mismatched lengths
my_list <- list(
  name  = c("Alice", "Bob"),
  score = c(85, 90, 78)
)
df <- bind_rows(my_list)
#   name score
# 1 Alice    85
# 2   Bob    90
# 3  <NA>    78

When you have a named vector rather than a list of data frames, use tibble::enframe() to pivot it into a two-column data frame with names and values. For the base R equivalent of row-binding a list of data frames, do.call(rbind, list_of_dfs) works but requires identical column structures.

See also