R list of lists to data.frame
The value of nrow needs to be fixed. I fixed your code as follows:
dd <- as.data.frame(matrix(unlist(listHolder), nrow=length(unlist(listHolder[1]))))
Converting a list of lists into a data.frame in R
What about this?
do <- as.data.frame(do.call(rbind, lapply(my.stuff, as.vector)))
do <- cbind(my.var=rownames(do), do)
do[do == "NULL"] <- NA
Result
> do
my.var my.col1 my.col2 my.col3 my.col4
AA AA 1 4 NA NA
BB BB NA NA NA NA
CC CC 13 8 2 10
DD DD NA NA -5 7
Edit:
If we don't want lists as column objects as @akrun reasonably suggests, we could do it this way:
u <- as.character(unlist(my.stuff, recursive=FALSE))
u[u == "NULL"] <- NA
do <- matrix(as.integer(u), nrow=4, byrow=TRUE,
dimnames=list(NULL, names(my.stuff[[1]])))
do <- data.frame(my.var=names(my.stuff), do, stringsAsFactors=FALSE)
Test:
> all.equal(str(do), str(desired.object))
'data.frame': 4 obs. of 5 variables:
$ my.var : chr "AA" "BB" "CC" "DD"
$ my.col1: int 1 NA 13 NA
$ my.col2: int 4 NA 8 NA
$ my.col3: int NA NA 2 -5
$ my.col4: int NA NA 10 7
'data.frame': 4 obs. of 5 variables:
$ my.var : chr "AA" "BB" "CC" "DD"
$ my.col1: int 1 NA 13 NA
$ my.col2: int 4 NA 8 NA
$ my.col3: int NA NA 2 -5
$ my.col4: int NA NA 10 7
[1] TRUE
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
Make dataframe from list of lists but each element a column
If the names are always a match one-by-one, you can simply do,
do.call(rbind, lapply(myList, unlist))
# L1 L2 L3 a1 a2 a3 b1 b2 b3
#[1,] 1 2 3 1 2 3 1 2 3
#[2,] 4 5 6 4 5 6 4 5 6
#[3,] 7 8 9 7 8 9 7 8 9
How to convert list of lists to dataframe in R
I couldn't reproduce your example, but what you're trying to do is simple. You could use do.call
to call the rbind
function on the list and what you get at the end is a pretty dataframe.
list <- getOptionChain("AAPL", "2019/2021")
data <- do.call(rbind, list)
Transforming a list of lists into dataframe
For every list in fruits
you can create a one row dataframe and bind the data.
dplyr::bind_rows(lapply(fruits, function(x) as.data.frame(t(sapply(x,
function(y) paste0(y, collapse = "+"))))))
# V1 V2 V3 V4
#1 orange pear <NA> <NA>
#2 pear+orange <NA> <NA> <NA>
#3 lemon+apple pear grape apple
extract one element/data frame from a list of lists
So after messing around, the answer seems to be very simple: `results1<-cj1["coefficients",]. This creates the list that I want. Akrun, if you read this, thank you for your support.
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)
Related Topics
Efficiently Sum Across Multiple Columns in R
Ggplot2, Facet_Grid, Free Scales
Data.Table - Select First N Rows Within Group
Converting Latitude and Longitude Points to Utm
How to Connect Two Coordinates with a Line Using Leaflet in R
How to Draw Stacked Bars in Ggplot2 That Show Percentages Based on Group
Unicode Characters in Ggplot2 PDF Output
Rolling Join on Data.Table with Duplicate Keys
Using Cut and Quartile to Generate Breaks in R Function
Assign Unique Id Based on Two Columns
How to Escape a Backslash in R
How to Generate a Matrix of Combinations
Different Size Facets Proportional of X Axis on Ggplot 2 R
How to Get Name of Variable in R (Substitute)
Moving Average of Previous Three Values in R
R - Use Rbind on Multiple Variables with Similar Names
Interpretation of Ordered and Non-Ordered Factors, VS. Numerical Predictors in Model Summary