Using tidyverse, you could use purrr to help you


library(dplyr)
library(purrr)

tibble(
  pair = map(lol, "pair"),
  genes_vec = map_chr(lol, "genes")
) %>% 
  mutate(
    pair1 = map_chr(pair, 1),
    pair2 = map_chr(pair, 2) 
  ) %>%
  select(pair1, pair2, genes_vec)
#> # A tibble: 3 x 3
#>        pair1     pair2 genes_vec
#>        <chr>     <chr>     <chr>
#> 1 BoneMarrow Pulmonary     PRR11
#> 2 BoneMarrow Umbilical    GNB2L1
#> 3  Pulmonary Umbilical    ATP1B1

with the second example, just replace map_chr(lol, "genes") with map(lol2, "genes") as you want to keep a nested dataframe with a list column.


tibble(
  pair = map(lol2, "pair"),
  genes_vec = map(lol2, "genes")
) %>% 
  mutate(
    pair1 = map_chr(pair, 1),
    pair2 = map_chr(pair, 2) 
  ) %>%
  select(pair1, pair2, genes_vec)
#> # A tibble: 3 x 3
#>        pair1     pair2 genes_vec
#>        <chr>     <chr>    <list>
#> 1 BoneMarrow Pulmonary <chr [2]>
#> 2 BoneMarrow Umbilical <chr [1]>
#> 3  Pulmonary Umbilical <chr [1]>

And a more generic approach would be to work with nested tibbles and unnest them as needed

library(dplyr)
library(purrr)
library(tidyr)

tab1 <-lol %>%
  transpose() %>%
  as_tibble() %>%
  mutate(pair = map(pair, ~as_tibble(t(.x)))) %>%
  mutate(pair = map(pair, ~set_names(.x, c("pair1", "pair2"))))
tab1
#> # A tibble: 3 x 2
#>               pair     genes
#>             <list>    <list>
#> 1 <tibble [1 x 2]> <chr [1]>
#> 2 <tibble [1 x 2]> <chr [1]>
#> 3 <tibble [1 x 2]> <chr [1]>

For lol2 nothing changes unless the list lol2 instead of lol1

tab2 <- lol2 %>%
  transpose() %>%
  as_tibble() %>%
  mutate(pair = map(pair, ~as_tibble(t(.x)))) %>%
  mutate(pair = map(pair, ~set_names(.x, c("pair1", "pair2"))))
tab2
#> # A tibble: 3 x 2
#>               pair     genes
#>             <list>    <list>
#> 1 <tibble [1 x 2]> <chr [2]>
#> 2 <tibble [1 x 2]> <chr [1]>
#> 3 <tibble [1 x 2]> <chr [1]>

You can then unnest what the column you want

tab1 %>%
  unnest()
#> # A tibble: 3 x 3
#>    genes      pair1     pair2
#>    <chr>      <chr>     <chr>
#> 1  PRR11 BoneMarrow Pulmonary
#> 2 GNB2L1 BoneMarrow Umbilical
#> 3 ATP1B1  Pulmonary Umbilical

tab2 %>% 
  unnest(pair)
#> # A tibble: 3 x 3
#>       genes      pair1     pair2
#>      <list>      <chr>     <chr>
#> 1 <chr [2]> BoneMarrow Pulmonary
#> 2 <chr [1]> BoneMarrow Umbilical
#> 3 <chr [1]>  Pulmonary Umbilical
Answer from cderv on Stack Overflow
Top answer
1 of 6
19

Using tidyverse, you could use purrr to help you


library(dplyr)
library(purrr)

tibble(
  pair = map(lol, "pair"),
  genes_vec = map_chr(lol, "genes")
) %>% 
  mutate(
    pair1 = map_chr(pair, 1),
    pair2 = map_chr(pair, 2) 
  ) %>%
  select(pair1, pair2, genes_vec)
#> # A tibble: 3 x 3
#>        pair1     pair2 genes_vec
#>        <chr>     <chr>     <chr>
#> 1 BoneMarrow Pulmonary     PRR11
#> 2 BoneMarrow Umbilical    GNB2L1
#> 3  Pulmonary Umbilical    ATP1B1

with the second example, just replace map_chr(lol, "genes") with map(lol2, "genes") as you want to keep a nested dataframe with a list column.


tibble(
  pair = map(lol2, "pair"),
  genes_vec = map(lol2, "genes")
) %>% 
  mutate(
    pair1 = map_chr(pair, 1),
    pair2 = map_chr(pair, 2) 
  ) %>%
  select(pair1, pair2, genes_vec)
#> # A tibble: 3 x 3
#>        pair1     pair2 genes_vec
#>        <chr>     <chr>    <list>
#> 1 BoneMarrow Pulmonary <chr [2]>
#> 2 BoneMarrow Umbilical <chr [1]>
#> 3  Pulmonary Umbilical <chr [1]>

And a more generic approach would be to work with nested tibbles and unnest them as needed

library(dplyr)
library(purrr)
library(tidyr)

tab1 <-lol %>%
  transpose() %>%
  as_tibble() %>%
  mutate(pair = map(pair, ~as_tibble(t(.x)))) %>%
  mutate(pair = map(pair, ~set_names(.x, c("pair1", "pair2"))))
tab1
#> # A tibble: 3 x 2
#>               pair     genes
#>             <list>    <list>
#> 1 <tibble [1 x 2]> <chr [1]>
#> 2 <tibble [1 x 2]> <chr [1]>
#> 3 <tibble [1 x 2]> <chr [1]>

For lol2 nothing changes unless the list lol2 instead of lol1

tab2 <- lol2 %>%
  transpose() %>%
  as_tibble() %>%
  mutate(pair = map(pair, ~as_tibble(t(.x)))) %>%
  mutate(pair = map(pair, ~set_names(.x, c("pair1", "pair2"))))
tab2
#> # A tibble: 3 x 2
#>               pair     genes
#>             <list>    <list>
#> 1 <tibble [1 x 2]> <chr [2]>
#> 2 <tibble [1 x 2]> <chr [1]>
#> 3 <tibble [1 x 2]> <chr [1]>

You can then unnest what the column you want

tab1 %>%
  unnest()
#> # A tibble: 3 x 3
#>    genes      pair1     pair2
#>    <chr>      <chr>     <chr>
#> 1  PRR11 BoneMarrow Pulmonary
#> 2 GNB2L1 BoneMarrow Umbilical
#> 3 ATP1B1  Pulmonary Umbilical

tab2 %>% 
  unnest(pair)
#> # A tibble: 3 x 3
#>       genes      pair1     pair2
#>      <list>      <chr>     <chr>
#> 1 <chr [2]> BoneMarrow Pulmonary
#> 2 <chr [1]> BoneMarrow Umbilical
#> 3 <chr [1]>  Pulmonary Umbilical
2 of 6
4

This should work:

data.frame(do.call(rbind,lol2))
data.frame(do.call(rbind,lol2))
                   pair         genes
1 BoneMarrow, Pulmonary GNB2L1, PRR11
2 BoneMarrow, Umbilical        GNB2L1
3  Pulmonary, Umbilical        ATP1B1

The way you treat the genes as a vector is the same way you can treat the pairs as a vector: instead of pair 1 and 2 you just use both of them.

🌐
Rstats-tips
rstats-tips.net › 2021 › 02 › 07 › converting-lists-of-lists-of-lists-to-data-frames-or-tibbles
Converting Lists of Lists of Lists to Data.Frames (or Tibbles) - rstats-tips.net
February 7, 2021 - Look’s nice. But the original list consists of 5 rows. The result above is twice as long. So what happend? as_tibble generates for each entry of the list of the inner lists a row. That’s not what I’ve expected.
Discussions

[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
r - How to convert a list of tibbles/dataframes into a nested tibble/dataframe - Stack Overflow
Releases Keep up-to-date on features we add to Stack Overflow and Stack Internal. ... Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Tidy solutions appreciated! ... library(tibble) enframe(ex_list... More on stackoverflow.com
🌐 stackoverflow.com
Combine a list of tibbles into a document term matrix
After creating my_list, you can do this: tibble(group = c("doc1", "doc2", "doc3"), data = my_list) %>% tidyr::unnest(data) %>% tidyr::pivot_wider(names_from = "token", values_from = "frequency") This will create a table: group | A | B | C | doc1 | 65 | 62 | 51 | doc2 | 75 | 64 | 56 | doc3 | 81 | 62 | 57 If you want to get fancy you can do this: listN <- function(...){ anonList <- list(...) names(anonList) <- as.character(substitute(list(...)))[-1] anonList } my_list <- listN(doc1, doc2, doc3) enframe(my_list, "group", "data") %>% tidyr::unnest(data) %>% tidyr::pivot_wider(names_from = "token", values_from = "frequency") The listN function provides a special way to make a list where each element of the list is named according to the name of the object that was used to make that element of the list (e.g. if the first element of the list will be the data frame stored in the variable doc1, the name of the first element in the list will also be made "doc1"). This is convenient because you don't have to explicitly write out the names of each element in the list (i.e. more automated, more convenient, and less error prone when combining many elements into a list). enframe then creates a new dataframe of two columns where one column is the name to each list element and the second column is the actual list element itself. More on reddit.com
🌐 r/Rlanguage
4
3
May 15, 2023
convert such tibble to data frame
I need to change shape of a data frame from long to wide. After reshaping, it results a tibble [1 x 7], each column in tibble is a list. I want to change tibble to data frame. Using code below, only first 3 columns (lists) can be done, rest of them are failed because lengths of these columns ... More on forum.posit.co
🌐 forum.posit.co
0
0
December 18, 2020
🌐
GeeksforGeeks
geeksforgeeks.org › convert-tibble-to-data-frame-in-r
Convert Tibble to Data Frame in R | GeeksforGeeks
April 28, 2025 - Below are effective methods to perform this conversion using base R and the readr package.Method 1: Convert One Column to NumericStart by checking t ... We have a list of values and if we want to Convert a List to a Dataframe within it, we can use a as.data.frame. it Convert a List to a Dataframe for each value. A DataFrame is a two-dimensional tabular data structure that can store different types of data. Various functions and packages, such as dat ... In this article, we are going to see how to print the entire tibble to R Console.
🌐
Robwiederstein
robwiederstein.org › 2022 › 04 › 13 › lists-into-tibbles-part-03
List into Tibble-Part-03 - Rob Wiederstein
April 13, 2022 - While it may be achieved within the traditional data frame (the base package), we will use the tibble package to accomplish the task. Converting a list to a tibble will require some knowledge of three packages: tibble, tidyr, and purrr package.
🌐
Tidyverse
tibble.tidyverse.org › reference › as_tibble.html
Coerce lists, matrices, and more to data frames — as_tibble • tibble
as_tibble() turns an existing object, such as a data frame or matrix, into a so-called tibble, a data frame with class tbl_df. This is in contrast with tibble(), which builds a tibble from individual columns. as_tibble() is to tibble() as base::as.data.frame() is to base::data.frame(). as_tibble() is an S3 generic, with methods for: data.frame: Thin wrapper around the list method that implements tibble's treatment of rownames. matrix, poly, ts, table Default: Other inputs are first coerced with base::as.data.frame(). as_tibble_row() converts a vector to a tibble with one row.
🌐
R for Data Science
r4ds.had.co.nz › tibbles.html
10 Tibbles - R for Data Science - Hadley Wickham
Throughout this book we work with “tibbles” instead of R’s traditional data.frame. Tibbles are data frames, but they tweak some older behaviours to make life a little easier. R is an old language, and some things that were useful 10 or 20 years ago now get in your way.
Find elsewhere
🌐
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!

🌐
Henrywang
henrywang.nl › transform-list-into-dataframe-with-tidyr-and-purrr
Transform List into Dataframe with tidyr and purrr | Henry Wang
January 21, 2021 - with 166 more rows · tibble(repo = gh_repos) %>% unnest_longer(repo) %>% hoist(repo, "name", "full_name") #> # A tibble: 176 x 3 #> name full_name repo #> <chr> <chr> <list> #> 1 after gaborcsardi/after <named list [66]> #> 2 argufy gaborcsardi/argufy <named list [66]> #> 3 ask gaborcsardi/ask <named list [66]> #> 4 baseimports gaborcsardi/baseimports <named list [66]> #> 5 citest gaborcsardi/citest <named list [66]> #> 6 clisymbols gaborcsardi/clisymbols <named list [66]> #> 7 cmaker gaborcsardi/cmaker <named list [66]> #> 8 cmark gaborcsardi/cmark <named list [66]> #> 9 conditions gaborcsardi/conditions <named list [66]> #> 10 crayon gaborcsardi/crayon <named list [66]> #> # …
🌐
Reddit
reddit.com › r/rlanguage › combine a list of tibbles into a document term matrix
r/Rlanguage on Reddit: Combine a list of tibbles into a document term matrix
May 15, 2023 -

Good evening everyone,

I'm struggling with transforming a list composed of tibbles (tokens and their frequencies) into a document term matrix, I mean, a matrix with documents per row and terms per column.

Think in something like this, for instance:

library(tibble)

# some mock up data
doc1 <- tribble(
  ~token, ~frequency,
  "A", 65,
  "B", 62,
  "C", 51,
)

doc2 <- tribble(
  ~token, ~frequency,
  "A", 75,
  "B", 64,
  "C", 56,
)

doc3 <- tribble(
  ~token, ~frequency,
  "A", 81,
  "B", 62,
  "C", 57,
)

# Combine the documents into a list
my_list <- list(doc1, doc2, doc3)

I tried something like this:

# Convert each tibble into a document term matrix
dtm_list <- lapply(my_list, function(x) {
  cast_sparse(x, token, frequency)
})

# Combine the document term matrices into a single sparse matrix
dtm <- Reduce(`+`, dtm_list)

But matrices must have the same dimensions and I stuck there.

Thank you in advance :)

🌐
Tidyverse
tibble.tidyverse.org
Simple Data Frames • tibble
This will work for reasonable inputs that are already data.frames, lists, matrices, or tables. You can also create a new tibble from column vectors with tibble(): tibble(x = 1:5, y = 1, z = x^2 + y) #> # A tibble: 5 × 3 #> x y z #> <int> <dbl> <dbl> #> 1 1 1 2 #> 2 2 1 5 #> 3 3 1 10 #> 4 4 1 17 #> 5 5 1 26 · tibble() does much less than data.frame(): it never changes the type of the inputs (e.g.
🌐
Tidyverse
tibble.tidyverse.org › reference › tibble.html
Build a data frame — tibble • tibble
to refer explicitly to columns or outside objects a <- 1 tibble(a = 2, b = a) #> # A tibble: 1 × 2 #> a b #> <dbl> <dbl> #> 1 2 2 tibble(a = 2, b = .data$a) #> # A tibble: 1 × 2 #> a b #> <dbl> <dbl> #> 1 2 2 tibble(a = 2, b = .env$a) #> # A tibble: 1 × 2 #> a b #> <dbl> <dbl> #> 1 2 1 tibble(a = 2, b = !!a) #> # A tibble: 1 × 2 #> a b #> <dbl> <dbl> #> 1 2 1 try(tibble(a = 2, b = .env$bogus)) #> Error : object 'bogus' not found try(tibble(a = 2, b = !!bogus)) #> Error in eval(expr, envir) : object 'bogus' not found # Use tibble_row() to construct a one-row tibble: tibble_row(a = 1, lm = lm(Height ~ Girth + Volume, data = trees)) #> # A tibble: 1 × 2 #> a lm #> <dbl> <list> #> 1 1 <lm>
🌐
Statology
statology.org › home › how to convert tibble to data frame in r (with example)
How to Convert Tibble to Data Frame in R (With Example)
April 21, 2022 - By default, the read_csv() function imports the CSV file as a tibble. However, we can use the following syntax to convert this tibble to a data frame: #convert tibble to data frame my_df <- as.data.frame(my_tibble) #view class of my_df class(my_df) [1] "data.frame"
🌐
Posit Community
forum.posit.co › general
convert such tibble to data frame - General - Posit Community
December 18, 2020 - I need to change shape of a data frame from long to wide. After reshaping, it results a tibble [1 x 7], each column in tibble is a list. I want to change tibble to data frame. Using code below, only first 3 columns (lis…
🌐
Posit Community
forum.posit.co › general
Converting list of lists of dataframes to a single tibble - General - Posit Community
September 23, 2021 - quantmod::getOptionChain( "SPY", NULL) returns a list (dated) of two lists (calls, puts) of various sized dataframes. I'm trying to convert this into a single (long) tibble, but due to the various sizes I can't use bind_rows and the only suggestion I see uses the obsolete plyr package.
🌐
Stack Overflow
stackoverflow.com › questions › 73105902 › convert-tibble-to-dataframe-in-r
tidyr - convert tibble to dataframe in r - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. I used the following code to separate some data in the following format: Copyd <- tibble(input=c("John Hopkins Institute 8.4 8.6 9.2 Blue", "Stanford : New School 9.4 5.6 9.2 Green", "Mayor College 6.4 7.6 4.2 Red")) d %>% extract(input, regex="(.+) ([.\\d]+) ([.\\d]+) ([.\\d]+) (.+)", into=c("College Names", "Food rating", "Critic Rating", "Student rating", "Color"))
🌐
Rdrr.io
rdrr.io › cran › tibble › man › as_tibble.html
as_tibble: Coerce lists, matrices, and more to data frames in tibble: Simple Data Frames
January 11, 2026 - as_tibble( x, ..., .rows = NULL, .name_repair = c("check_unique", "unique", "universal", "minimal", "unique_quiet", "universal_quiet"), rownames = pkgconfig::get_config("tibble::rownames", NULL) ) ## S3 method for class 'data.frame' as_tibble( x, validate = NULL, ..., .rows = NULL, .name_repair = c("check_unique", "unique", "universal", "minimal", "unique_quiet", "universal_quiet"), rownames = pkgconfig::get_config("tibble::rownames", NULL) ) ## S3 method for class 'list' as_tibble( x, validate = NULL, ..., .rows = NULL, .name_repair = c("check_unique", "unique", "universal", "minimal", "uniqu
🌐
Jennybc
jennybc.github.io › purrr-tutorial › ls13_list-columns.html
List columns
Put the variables needed for country-specific models into nested dataframe. In a list-column! Use the usual “map inside mutate”, possibly with the broom package, to pull interesting information out of the 142 fitted linear models. Nest the data frames, i.e. get one meta-row per country: gap_nested <- gapminder %>% group_by(country) %>% nest() gap_nested #> # A tibble: 142 x 2 #> # Groups: country [142] #> country data #> <fct> <list<df[,5]>> #> 1 Afghanistan [12 × 5] #> 2 Albania [12 × 5] #> 3 Algeria [12 × 5] #> 4 Angola [12 × 5] #> 5 Argentina [12 × 5] #> 6 Australia [12 × 5] #> 7 Austria [12 × 5] #> 8 Bahrain [12 × 5] #> 9 Bangladesh [12 × 5] #> 10 Belgium [12 × 5] #> # …