Home > Enterprise >  Combine list elements into a dataframe r
Combine list elements into a dataframe r

Time:05-26

I currently have a list with columns as individual elements.

I would like to combine list elements with the same column names (i.e. bind rows) and merge across the different columns (i.e. bind columns) into a single data frame. I'm having difficulty finding examples of how to do this.

l = list(est = c(0, 0.062220390087795, 1.1020213968139, 0.0359939361491544
), se = c(0.0737200634874046, 0.237735179934829, 0.18105632705918, 
0.111359438298789), rf = structure(c(NA, NA, NA, 4L), levels = c("Never\nsmoker", 
"Occasional\nsmoker", "Ex-regular\nsmoker", "Smoker"), class = "factor"), 
    n = c(187L, 18L, 32L, 82L), model = c("Crude", "Crude", "Crude", 
    "Crude"), est = c(0, 0.112335510453586, 0.867095253670329, 
    0.144963556944891), se = c(0.163523775933409, 0.237039485900481, 
    0.186247776987999, 0.119887623484768), rf = structure(c(NA, 
    NA, NA, 4L), levels = c("Never\nsmoker", "Occasional\nsmoker", 
    "Ex-regular\nsmoker", "Smoker"), class = "factor"), n = c(187L, 
    18L, 32L, 82L), model = c("Model 1", "Model 1", "Model 1", 
    "Model 1"), est = c(0, 0.107097305324242, 0.8278765140371, 
    0.0958220447859447), se = c(0.164787596943329, 0.237347836229364, 
    0.187201880036661, 0.120882616647714), rf = structure(c(NA, 
    NA, NA, 4L), levels = c("Never\nsmoker", "Occasional\nsmoker", 
    "Ex-regular\nsmoker", "Smoker"), class = "factor"), n = c(187L, 
    18L, 32L, 82L), model = c("Model 2", "Model 2", "Model 2", 
    "Model 2"))

I would like the data to have the following format:

data.frame(
 est = c(),
 se = c(),
 rf = c(),
 model = c()
)

Any help would be appreciated. Thank you!

CodePudding user response:

In this solution, first the elements of l are grouped by name and then are combined using c. Finally, the resulting list is converted to a dataframe using map_dfc.

library(dplyr)
library(purrr)

cols <- c("est", "se", "rf", "model")

setNames(cols,cols) |>
    map(~l[names(l) == .x]) |>
    map_dfc(~do.call(c, .x))


#> # A tibble: 12 × 4
#>       est     se rf     model  
#>     <dbl>  <dbl> <fct>  <chr>  
#>  1 0      0.0737 NA     Crude  
#>  2 0.0622 0.238  NA     Crude  
#>  3 1.10   0.181  NA     Crude  
#>  4 0.0360 0.111  Smoker Crude  
#>  5 0      0.164  NA     Model 1
#>  6 0.112  0.237  NA     Model 1
#>  7 0.867  0.186  NA     Model 1
#>  8 0.145  0.120  Smoker Model 1
#>  9 0      0.165  NA     Model 2
#> 10 0.107  0.237  NA     Model 2
#> 11 0.828  0.187  NA     Model 2
#> 12 0.0958 0.121  Smoker Model 2

CodePudding user response:

another option

library(purrr)

grp <- (seq(length(l)) - 1) %/% 5
l_split <- split(l, grp)
map_df(l_split, c)

#> # A tibble: 12 × 5
#>       est     se rf         n model  
#>     <dbl>  <dbl> <fct>  <int> <chr>  
#>  1 0      0.0737 <NA>     187 Crude  
#>  2 0.0622 0.238  <NA>      18 Crude  
#>  3 1.10   0.181  <NA>      32 Crude  
#>  4 0.0360 0.111  Smoker    82 Crude  
#>  5 0      0.164  <NA>     187 Model 1
#>  6 0.112  0.237  <NA>      18 Model 1
#>  7 0.867  0.186  <NA>      32 Model 1
#>  8 0.145  0.120  Smoker    82 Model 1
#>  9 0      0.165  <NA>     187 Model 2
#> 10 0.107  0.237  <NA>      18 Model 2
#> 11 0.828  0.187  <NA>      32 Model 2
#> 12 0.0958 0.121  Smoker    82 Model 2
  • Related