Denver Dang
Denver Dang

Reputation: 2605

Multiple lists into one data frame in R

So I'm running a package in which the output of the function I'm using is something similar to this:

        area     ID   structure
1       150       1     house

I have several of these which I get by looping through some stuff. Basically this is my loop function:

for (k in 1:length(models)) {
    for (l in 1:length(patients)) {
        print(result[[l]][[k]])
        tableData[[l]][[k]] <- do.call(rbind, result[[l]][[k]])
    }
}

So the print(result[[l]][[k]]) gives the output I showed you in the beginning. So my issue is to put all of these into one dataframe. And so far it just doesn't work, i.e. the do.call function, which I have read is the one to use when combining lists into dataframes.

So where am I going wrong here ?

Updated:

dput() output (area = value in this case):

list(list(structure(list(value = 0.0394797760472196, ID = "1 house", 
    structure = "house", model = structure(1L, .Label = "wood", class = "factor")), .Names = c("value", 
"ID", "structure", "model"), row.names = c(NA, -1L), class = "data.frame"), 
    structure(list(value = 0.0394797760472196, ID = "1 house", 
        structure = "house", model = structure(1L, .Label = "stone", class = "factor")), .Names = c("value", 
    "ID", "structure", "model"), row.names = c(NA, -1L), class = "data.frame")), 
    list(structure(list(value = 0.0306923865158472, ID = "2 house", 
        structure = "house", model = structure(1L, .Label = "wood", class = "factor")), .Names = c("value", 
    "ID", "structure", "model"), row.names = c(NA, -1L), class = "data.frame"), 
        structure(list(value = 0.0306923865158472, ID = "2 house", 
            structure = "house", model = structure(1L, .Label = "stone", class = "factor")), .Names = c("value", 
        "ID", "structure", "model"), row.names = c(NA, -1L
        ), class = "data.frame")))
list(list(structure(list(value = 0.0394797760472196, ID = "1 house", 
    structure = "house", model = structure(1L, .Label = "wood", class = "factor")), .Names = c("value", 
"ID", "structure", "model"), row.names = c(NA, -1L), class = "data.frame"), 
    structure(list(value = 0.0394797760472196, ID = "1 house", 
        structure = "house", model = structure(1L, .Label = "stone", class = "factor")), .Names = c("value", 
    "ID", "structure", "model"), row.names = c(NA, -1L), class = "data.frame")), 
    list(structure(list(value = 0.0306923865158472, ID = "2 house", 
        structure = "house", model = structure(1L, .Label = "wood", class = "factor")), .Names = c("value", 
    "ID", "structure", "model"), row.names = c(NA, -1L), class = "data.frame"), 
        structure(list(value = 0.0306923865158472, ID = "2 house", 
            structure = "house", model = structure(1L, .Label = "stone", class = "factor")), .Names = c("value", 
        "ID", "structure", "model"), row.names = c(NA, -1L
        ), class = "data.frame")))

Upvotes: 0

Views: 3032

Answers (1)

zlipp
zlipp

Reputation: 801

Edit: I initially used purrr::map_dfr to solve this problem, but purrr::reduce is much more appropriate.

The list nesting means we have to bind rows together twice. Here's a solution using the purrr and dplyr packages and assigning your dput list to the variable my_list:


library(purrr)
library(dplyr)

my_df <- reduce(my_list, bind_rows)
#> Warning in bind_rows_(x, .id): Unequal factor levels: coercing to character
#> Warning in bind_rows_(x, .id): binding character and factor vector,
#> coercing into character vector

#> Warning in bind_rows_(x, .id): binding character and factor vector,
#> coercing into character vector
#> Warning in bind_rows_(x, .id): Unequal factor levels: coercing to character
#> Warning in bind_rows_(x, .id): binding character and factor vector,
#> coercing into character vector

#> Warning in bind_rows_(x, .id): binding character and factor vector,
#> coercing into character vector
my_df
#>        value      ID structure model
#> 1 0.03947978 1 house     house  wood
#> 2 0.03947978 1 house     house stone
#> 3 0.03069239 2 house     house  wood
#> 4 0.03069239 2 house     house stone

I find map-ing with purrr way more intuitive than do.call. Let me know if this helps!

Upvotes: 4

Related Questions