1

I already referred all this post1, post2, post3 etc. So, please don't mark it as duplicate.

As you can see below, my list of lists has 3 levels. (List within a list within a list). Some of the lists doesn't have child list (ex: visits, procedures)

enter image description here

I don't know how to create list of lists properly. Hence the screenshot above.

I would like to transform the list of lists to a data frame. However, I am not able to transform it to data frame properly. can you help me, please?

Below is what I tried

df = do.call(rbind, dataFcases) # approach 1
df= as.data.frame(t(as.data.frame(dataFcases))) #approach 2
df <- data.frame(matrix(unlist(dataFcases), nrow=length(dataFcases), byrow=T)) #approach 3

I don't know how to create sample data for 3 levels as I am new to R. Can you please help me convert this to a data frame?

The Great
  • 7,215
  • 7
  • 40
  • 128
  • Does [this answer](https://stackoverflow.com/questions/59905687/nested-list-to-dataframe-using-purrr-map) help? Posting example data we could work with would greatly impact the usefulness of potential answers. – missuse Oct 22 '20 at 10:09
  • Unfortunately, I don't know how to create sample data for this scenario. The above answer that you suggested used column names bt I may not know my columns name in advance.. – The Great Oct 22 '20 at 10:12
  • 1
    But how can we write answers if we do not know the content (structure) of each sublist? – missuse Oct 22 '20 at 10:20
  • Most likely if you could figure out how to write a reproducible example you would figure out how to do the requested transformation. – missuse Oct 22 '20 at 10:29
  • Can you please explain why you do not use `list` when you want to create a list? Lists can contain a disparate collection of data types, while dataframes are for row/column layout. – cdalitz Oct 22 '20 at 11:19
  • Have you tried using `bind_rows` (you'd need to use 3 times i think given there are 3 levels. If you like you can also use some `.id` as data.frame identifier. – Dayne Oct 22 '20 at 14:17

1 Answers1

1

A dummy database:

for (i in c(1:6)){
  c1=paste0('a',i)
  c2=paste0('b',i)
  assign(paste0('df',i),tibble(!!c1 := rnorm(50), !!c2 := rnorm(50)))
}
L3=list(list(df1,df2,df3),list(df4,df5),df6)

This looks like: enter image description here

Using bind_rows:

> L3 %>%
+   bind_rows(.id='df_identifier')
# A tibble: 300 x 13
   df_identifier      a1      b1    a2    b2    a3    b3    a4    b4    a5    b5    a6    b6
   <chr>           <dbl>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1 1             -0.222  -0.744     NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
 2 1             -1.09   -0.307     NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
 3 1             -0.520   0.160     NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
 4 1              1.13    0.380     NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
 5 1             -1.26   -2.17      NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
 6 1              0.575   0.0486    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
 7 1              0.0447 -1.48      NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
 8 1             -0.735   0.730     NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
 9 1             -0.360  -0.143     NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
10 1              0.240  -0.245     NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
# ... with 290 more rows
Dayne
  • 463
  • 3
  • 12