You can also use (at least v1.9.3) of rbindlist in the data.table package:

library(data.table)

rbindlist(mylist, fill=TRUE)

##      Hit Project Year Rating      Launch  ID    Dept            Error
## 1:  True    Blue 2011      4 26 Jan 2012  19 1, 2, 4               NA
## 2: False      NA   NA     NA          NA  NA      NA Record not found
## 3:  True   Green 2004      8 29 Feb 2004 183    6, 8               NA
Answer from hrbrmstr on Stack Overflow
🌐
Bioinformatics Answers
biostars.org › p › 9538122
Converting elements of a nested list into individual data frames in R
September 12, 2022 - For a data.frame, however, indeed you can't, so you need to assign each separately to the Global Environment: mapply(function(element, index) { assign( x = paste0("dataframe", index), value = as.data.frame(element), env = globalenv() ) return(NULL) }, nested_list, seq_along(nested_list))
Discussions

Converting elements of a nested list into individual data frames
This nested list contains 3 different lists that I need to convert to individual dataframes. x More on forum.posit.co
🌐 forum.posit.co
1
0
September 12, 2022
[Q] an efficient way to convert lists nested within a list to data.frame?
I would look into the flatten function in the purrr package. More on reddit.com
🌐 r/rstats
7
8
January 1, 2022
Problems with Column headers in converting a nested list into a dataframe
If I understand correctly, seeing your example, this is not a dataframe with a list column, but a simple (not nested) list of dataframes, each dataframe containing a unique column (named value or whatever, it doesn't seem to be important). And the result you want is a simple dataframe, with each of the dataframes' unique column as columns of that dataframe, named with the original name of the dataframe in the list. Here's a simple example that would answer that problem: l <- list(a = data.frame(value = 1:10), b = data.frame(value = letters[1:10]), c = data.frame(value = LETTERS[1:10])) # a simple example list l2 <- setNames(as.data.frame(l), names(l)) as.data.frame converts the list to a dataframe (granted all dataframes in the list have the same number of rows). setNames sets the names of the columns to the names of the dataframes in the list (granted all dataframes in the list contain only one column). More on reddit.com
🌐 r/rstats
3
3
October 23, 2021
r - Extracting from Nested list to data frame - Stack Overflow
I will put dput of what my list looks like at the bottom such that the q can be reproducible. The dput is of a not x. I have a big nested list called x that I'm trying to build a data frame from but More on stackoverflow.com
🌐 stackoverflow.com
January 16, 2017
🌐
Reddit
reddit.com › r/rstats › convert deeply nested list to dataframe
r/rstats on Reddit: Convert deeply nested list to dataframe
November 21, 2023 -

I have a deeply nested list like this:

results <- list()
results[[1]] <- list()
results[[2]] <- list()
results[[1]][["records"]] <- list()
results[[1]][["records"]][[1]] <- list(
  "ID" = "askdhgk",
  "vars" = list(
    "V1" = "Value 2",
    "V2" = "Value 3"
  )
)
results[[1]][["records"]][[2]] <- list(
  "ID" = "adsfaw3w",
  "vars" = list(
    "V1" = "Value 5",
    "V2" = "Value 6"
  )
)
results[[2]][["records"]][[1]] <- list(
  "ID" = "rtyu",
  "vars" = list(
    "V1" = "Value 8",
    "V2" = "Value 9"
  )
)
results[[2]][["records"]][[2]] <- list(
  "ID" = "324564",
  "vars" = list(
    "V1" = "Value 11",
    "V2" = "Value 12"
  )
)

I want a dataframe, and I've written this code to get it to the format I want:

library(dplyr)
for(i in seq_along(results)){
    for(n in seq_along(results[[i]][["records"]])){
      # overwrite the the record list with a dataframe of records
      results[[i]][["records"]][[n]] <- data.frame(as.list(unlist(results[[i]][["records"]][[n]])))
    }
    # overwrite the record entry with one dataframe of all records
    results[[i]] <- dplyr::bind_rows(results[[i]][["records"]])
  }
  # return a dataframe of all the records
  df1 <- dplyr::bind_rows(results)

I can't help but feel like this is overly complicated and not 'R-like' way of achieving this, but can't find a one-liner that does it. Any ideas?

🌐
Statistics Globe
statisticsglobe.com › home › learn r programming (tutorial & examples) | free introduction › convert nested lists to data frame or matrix in r (2 examples)
R Convert Nested Lists to Data Frame or Matrix (2 Examples) | List of Lists
March 21, 2022 - Example 2 shows how to bind the sub-lists of a nested list as rows in a matrix object. For this, we can use the do.call and rbind functions as shown below: my_list_data_rbind <- do.call(rbind, # Convert nested list to matrix by row my_nested_list) my_list_data_rbind # Print nested list in matrix # [,1] [,2] [,3] # l1 Integer,5 Character,3 "x" # l2 Integer,6 Character,3 "y" # l3 "This" "is another" "list"
🌐
GeeksforGeeks
geeksforgeeks.org › r language › convert-nested-lists-to-dataframe-in-r
Convert Nested Lists to Dataframe in R - GeeksforGeeks
November 15, 2021 - Method 1: To convert nested list to Data Frame by column. ... Create dataframe using data.frame function with the do.call and cbind.
🌐
CRAN
cran.r-project.org › web › packages › recombinator › recombinator.pdf pdf
Recombinate Nested Lists to Dataframes
A mini-utility package for turning nested lists into data.frames. A recombinator attempts to convert a depth 2 nested list into a data.frame.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › how-to-create-data-frame-using-nested-list-elements-in-r
How to create data frame using nested list elements in R?
June 3, 2021 - To create data frame using nested list elements, we would need to unlist the list elements and store them in a matrix then read as a data frame using data.frame function. For example, if we have a nested called LIST then the data frame can be crea
🌐
GeeksforGeeks
geeksforgeeks.org › r language › create-data-frame-from-nested-lapplys
Create data.frame from nested lapply's - GeeksforGeeks
July 23, 2025 - ... # Step 1: Create a nested list nested_list <- list( list(1, 2, 3), list(4, 5, 6), list(7, 8, 9) ) # Step 2: Apply nested lapply functions processed_list <- lapply(nested_list, function(x) { lapply(x, function(y) { y * 2 }) }) # Step 3: Convert ...
🌐
Reddit
reddit.com › r/rstats › [q] an efficient way to convert lists nested within a list to data.frame?
r/rstats on Reddit: [Q] an efficient way to convert lists nested within a list to data.frame?
January 1, 2022 -

Hi all,

Say I have a list object, where each element is a 'person' who is identifiable by their ID number. Then within each person is another list object which contains the actual vector of data.

Example: say I have ID numbers for person "AA001", "BB001", "CC001"

my_list = list(
  AA001 = list(x = 1:5, y = 1:5),
  BB001 = list(x = -1:-5, y = -1:-5),
  CC001 = list(x = rep(99, 5), y = rep(0, 5))
)
print(my_list)

My desired output is something like

my_df = data.frame(
  ID = rep(c("AA001", "BB001", "CC001"), each = 5),
  x = c(1:5, -1:-5, rep(99, 5)),
  y = c(1:5, -1:-5, rep(0, 5))
)
print(my_df)

If possible, I would prefer efficiency over readability as I will be running some simulations with thousands for IDs and thousands of runs.

As a side question, the way my_list is structured. Is that even a good way to store data?

Thank you in advance!

🌐
CRAN
cran.r-project.org › web › packages › tidyr › vignettes › nest.html
nested data frames - CRAN - R Project
You can create simple nested data ... not just in tibbles, but it’s considerably more work because the default behaviour of data.frame() is to treat lists as lists of columns.)...
🌐
Tender Is The Byte
tenderisthebyte.com › blog › 2019 › 11 › 02 › notes-from-the-messyverse-tidying-nested-lists
Notes from the Messyverse: How to tidy nested lists in R | Tender Is The Byte
November 2, 2019 - In general, map returns a vector the same length as its first argument (e.g., map returns a list, map_int returns an integer vector, and map_dfr returns a data frame by row binding.) Since you want each of the nested lists to be a row in your data frame, you’ll need map_dfr.
🌐
tidyr
tidyr.tidyverse.org › articles › nest.html
Nested data • tidyr
You can create simple nested data ... it’s considerably more work because the default behaviour of data.frame() is to treat lists as lists of columns.)...
🌐
Reddit
reddit.com › r/rstats › problems with column headers in converting a nested list into a dataframe
r/rstats on Reddit: Problems with Column headers in converting a nested list into a dataframe
October 23, 2021 -

Hi all,

I am trying to convert a nested list (tibbles) output from a db query into a regular data frame or matrix.

The nested list information is here.

db query output as nested list

I have found various ways to turn the elements of the nested list into columns in the df.

The difficulty is having each tibble's name assigned as the column header. Usually, with all the approaches, R puts as the column header …."value", "value.1" and "value.2".

I have gotten closest in an approach that resulted in the following as column headers...

“Lab_Test.value”, “LS.value”, “TW_Analyte_Comment.value”

I would like the column headers to be the names of the nested tibbles or “Lab_Test”, “LS” and “TW_Analyte_Comment”.

I would guess my error is that I am not referring to the proper part of each of the nested lists in the approach I take to do the conversion.

Any ideas?

Jose

Top answer
1 of 2
1

Maybe this can help

library(dplyr)
library(tidyr)

a <- unlist(a)

df <- data.frame(a=a, b=names(a)) %>% mutate(key=cumsum(b=="experience.duration")) %>% 
      split(.$key) %>% lapply(function(x) x %>% select(-key) %>% spread(b, a)) %>% 
      do.call(rbind, .) %>% t %>% data.frame

df$key <- rownames(df)

Then you can filter in on the rows of interest

The above would be equivalent to

rbind(unlist(a)[1:8], unlist(a)[9:16],unlist(a)[17:24]) %>% t

Update

try this for dput2

a <- unlist(dput2)

library(dplyr)
library(tidyr)

dummydf <- data.frame(b=c("experience.start", "experience.end", "experience.roleName", "experience.summary", 
                      "experience.org", "experience.org.name",  "experience.org.url", 
                      "_meta.weight", "_meta._sources._origin", "experience.duration"), key=1:10)


df <- data.frame(a=a, b=names(a))

df2 <- left_join(df, dummydf)
df2$key2 <- as.factor(cumsum(df2$key < c(0, df2$key[-length(df2$key)])) +1)
df_split <- split(df2, df2$key2)
df3 <- lapply(df_split, function(x){
       x %>% select(-c(key, key2)) %>% spread(b, a)
       }) %>% data.table::rbindlist(fill=TRUE) %>% t

df3 <- data.frame(df3)
i <- sapply(seq_along(dput2), function(y) rep(y, sapply(dput2, function(x) length(x))[y])) %>% unlist
names(df3) <- paste0(names(df3), "_", i)

View(df3)
2 of 2
0

Managed to figure something out, using dput3 above:

a <- dput3

aa <- lapply(1:length(a), function(y){tryCatch(lapply(1:length(a[[y]]), 
  function(i){if(is.null(a[[y]][[i]]$experience$start)){"Null"}else{a[[y]][[i]]$experience$start}}),error=function(e) print(list()))})


for(i in 1:length(aa)){for(y in 1:length(aa[[i]])){tryCatch(for(z in length(aa[[i]][[y]]))
     {test <- rbind(test, data.frame(key = i, key2= y))},error=function(e) print(0))}}

aaa <- unlist(aa)
df <- data.frame(a=aaa)
df2 <- cbind(df, test)
i <- sapply(seq_along(aa), function(y) rep(y, sapply(aa, function(x) length(x))[y])) %>% unlist

df5 <- data.frame(dates = df2$a)
df5 <- t(df5)
df5 <- data.frame(df5)
names(df5) <- paste0(names(df5), "_", i)
df5[] <- lapply(df5[], as.character)
l1 <- lapply(split(stack(df5), as.numeric(sub('.*_', '', stack(df5)[,2]))), '[', 1)
df6 <- t(do.call(cbindPad, l1))
df6 <- data.frame(df6)

Will try and expand it so it works with more than one vertical (as currently in aa I isolate start)

Top answer
1 of 5
10

Borrowing from Spacedman and flodel here, we can define the following pair of recursive functions:

library(tidyverse)  # I use dplyr and purrr here, plus tidyr further down below

depth <- function(this) ifelse(is.list(this), 1L + max(sapply(this, depth)), 0L)

bind_at_any_depth <- function(l) {
  if (depth(l) == 2) {
    return(bind_rows(l))
  } else {
    l <- at_depth(l, depth(l) - 2, bind_rows)
    bind_at_any_depth(l)
  }
}

We can now bind any arbitrary depth list into a single data.frame:

bind_at_any_depth(x)
# A tibble: 2 × 2
      a     b
  <dbl> <dbl>
1     1     2
2     3     4
bind_at_any_depth(x_ext) # From P Lapointe
# A tibble: 5 × 2
      a     b
  <dbl> <dbl>
1     1     2
2     5     6
3     7     8
4     1     2
5     3     4

If you want to keep track of the origin of each row, you can use this version:

bind_at_any_depth2 <- function(l) {
  if (depth(l) == 2) {
    l <- bind_rows(l, .id = 'source')
    l <- unite(l, 'source', contains('source'))
    return(l)
  } else {
    l <- at_depth(l, depth(l) - 2, bind_rows, .id = paste0('source', depth(l)))
    bind_at_any_depth(l)
  }
}

This will add a source column:

bind_at_any_depth2(x_ext)
# A tibble: 5 × 3
  source     a     b
*  <chr> <dbl> <dbl>
1  X_x_1     1     2
2  X_y_z     5     6
3 X_y_zz     7     8
4  Y_x_1     1     2
5  Y_y_1     3     4

Note: At some point you can use purrr::depth, and will need to change at_depth to modify_depth when their new version rolls out to CRAN (thanks @ManuelS).

2 of 5
3

UPDATE

Here's a way to flatten more deeply nested lists simply with unlist. Since the structure is now uneven, the result will not be a data.frame.

x_ext <- list(X = list(x = list(a = 1,
                       b = 2),
              y = list(z=list(a = 5,
                       b = 6),
                       zz=list(a = 7,
                       b = 8))),
     Y = list(x = list(a = 1,
                       b = 2),
              y = list(a = 3,
                       b = 4)))

unlist(x_ext)

   X.x.a    X.x.b  X.y.z.a  X.y.z.b X.y.zz.a X.y.zz.b    Y.x.a    Y.x.b    Y.y.a    Y.y.b 
       1        2        5        6        7        8        1        2        3        4 

My initial answer was unlist first and rbind aftrerwards. However, it works only with the example in the question.

x_unlist <- unlist(x, recursive = FALSE)
do.call("rbind", x_unlist)
    a b
X.x 1 2
X.y 3 4
Y.x 1 2
Y.y 3 4
🌐
Reddit
reddit.com › r/rprogramming › problems with column headers in converting a nested list into a dataframe
r/rprogramming on Reddit: Problems with Column headers in converting a nested list into a dataframe
October 23, 2021 -

Hi all,

I am trying to convert a nested list (tibbles) output from a db query into a regular data frame or matrix.

The nested list information is here.

db query output as nested list

I have found various ways to turn the elements of the nested list into columns in the df.

The difficulty is having each tibble's name assigned as the column header. Usually, with all the approaches, R puts as the column header …."value", "value.1" and "value.2".

I have gotten closest in an approach that resulted in the following as column headers...

“Lab_Test.value”, “LS.value”, “TW_Analyte_Comment.value”

I would like the column headers to be the names of the nested tibbles or “Lab_Test”, “LS” and “TW_Analyte_Comment”.

I would guess my error is that I am not referring to the proper part of each of the nested lists in the approach I take to do the conversion.

Any ideas?

Jose