Converting a list of lists to a dataframe in R: The Tidyverse-way
You could use
purrr::map_df(list_of_lists, tibble::as_tibble)
# A tibble: 10 x 3
# a b sum
# <dbl> <dbl> <dbl>
# 1 1 1 2
# 2 2 3 5
# 3 3 5 8
# 4 4 7 11
# 5 5 9 14
# 6 6 11 17
# 7 7 13 20
# 8 8 15 23
# 9 9 17 26
#10 10 19 29
Convert list of lists into single nested row of a data.frame or tibble (R)
You can use enframe
+ pivot_wider
tibble::enframe(data) %>% tidyr::pivot_wider()
# a b c
# <list> <list> <list>
#1 <list [3]> <list [1]> <list [2]>
To get length one column as vector we can add :
library(dplyr)
tibble::enframe(data) %>%
tidyr::pivot_wider() %>%
summarise(across(.fns = ~if(length(unlist(.)) == 1) unlist(.) else .))
# a b c
# <list> <chr> <list>
#1 <list [3]> foo <list [2]>
Convert named list of lists to dataframe using tidy approach
We may use bind_rows
which have the .id
that creates a new column from the names
of the list
library(dplyr)
bind_rows(df, .id = "fitname")
# A tibble: 4 × 6
fitname term estimate std.error statistic p.value
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 e1m1_fit (Intercept) 2.7 0.03 88.0 0.01
2 e1m1_fit log10(q) -0.1 0.01 -15.6 0.01
3 e2m2a_fit (Intercept) 2.7 0.03 79.8 0.01
4 e2m2a_fit log10(q) -0.1 0.01 -15.5 0.01
In addition, if the df
list
was created by looping with map
, the _dfr
can return a single tibble/data.frame
with the .id
specified as the name of the list
library(purrr)
map_dfr(yourlist, ~ yourfun(.x), .id = "fitname")
Convert a list to a data frame
Update July 2020:
The default for the parameter stringsAsFactors
is now default.stringsAsFactors()
which in turn yields FALSE
as its default.
Assuming your list of lists is called l
:
df <- data.frame(matrix(unlist(l), nrow=length(l), byrow=TRUE))
The above will convert all character columns to factors, to avoid this you can add a parameter to the data.frame() call:
df <- data.frame(matrix(unlist(l), nrow=132, byrow=TRUE),stringsAsFactors=FALSE)
How to convert list of list into tidy tibble or data.frame in R
One way using base R:
mylist <- lapply(1:2, function(i) {
#this is the important bit where you extract the corresponding elements
#of sample 1 first and sample 2 second.
df <- data.frame(lapply(my_lol, '[', i))
names(df) <- c('CoolFactorScore', 'CRT', 'SR', 'Sample')
df$CellType <- rownames(df)
row.names(df) <- NULL
df
})
do.call(rbind, mylist)
Out:
CoolFactorScore CRT SR Sample CellType
1 0.164477631 0.1331186 0.40707688 Sample1 B
2 0.198253819 0.1331186 0.40707688 Sample1 Mac
3 0.396414447 0.1331186 0.40707688 Sample1 NK
4 0.133118604 0.1331186 0.40707688 Sample1 Neu
5 0.107735498 0.1331186 0.40707688 Sample1 Stro
6 0.186215537 0.1840853 0.08291817 Sample2 B
7 0.184085292 0.1840853 0.08291817 Sample2 Mac
8 0.375349920 0.1840853 0.08291817 Sample2 NK
9 0.247664923 0.1840853 0.08291817 Sample2 Neu
10 0.006684328 0.1840853 0.08291817 Sample2 Stro
How to convert a list of tibbles/dataframes into a nested tibble/dataframe
We may use enframe
library(tibble)
enframe(ex_list)
# A tibble: 2 x 2
name value
<chr> <list>
1 a <tibble [4 × 2]>
2 b <df [32 × 11]>
If we need to change the column names, use the name
and value
> enframe(ex_list, name = 'data_name', value = 'data')
# A tibble: 2 x 2
data_name data
<chr> <list>
1 a <tibble [4 × 2]>
2 b <df [32 × 11]>
Convert Nested list into single data frame or tibble
To get the data in one dataframe you can use :
newdata <- do.call(rbind, lapply(data, function(x) do.call(cbind, x)))
Or using purrr
:
newdata <- purrr::map_df(data, ~do.call(cbind, .x))
Convert a large list of tibbles into a single tibble
I think you can have a try to passing this large list into data.table::rbindlist()
Related Topics
R - Download Filtered Datatable
Custom Ggplot2 Axis and Label Formatting
R Looping Through in Survey Package
How to Run a Function Every Second
Dplyr: Grouping and Summarizing/Mutating Data with Rolling Time Windows
Inline Function Code Doesn't Compile
Regression Line for the Entire Data Set Together with Regression Lines Based on Groups
Tidyr Separate Only First N Instances
Why Is Date Is Being Returned as Type 'Double'
Converting Date Column in Data Frame
As.Posixct Gives an Unexpected Timezone
R Dataframe with Varied Column Lengths
Why Does ".." Work to Pass Column Names in a Character Vector Variable
How to Add Main Title and Manipulating Axis Labels in Ggplot2 in Rstudio