Home > database >  how to convert a list to a data.frame in R?
how to convert a list to a data.frame in R?

Time:08-24

I have a list of data.frames with a rowname (sample-01) and 1 column (elem1), If I want to generate a data.frame with a list that contains data.frames with the same number of rows, it will be easy

do.call(cbind, mylist)

           elem1 elem2 elem3
sample-01   E7   E1   E1
sample-02   E7   E1   E1
sample-03   E4   E8   E8
sample-04   E8   E3   E2
sample-05   E9   E3   E2
sample-06   E8   E3   E2
sample-07   E5   E6   E7
sample-08   E8   E3   E2

but if I have a different number of rows,

mylist 

$elem1
            elem1
sample-01   E7
sample-02   E7
sample-03   E4
sample-04   E8
sample-05   E9
sample-06   E8


$elem2
            elem2
sample-01   E1
sample-02   E1
sample-03   E8
sample-04   E3



$elem3
            elem3
sample-05   E2
sample-06   E2
sample-07   E7
sample-08  E2

how can I generate the data.frame, using all the row.names in this case will be from sample-01 to sample-08, and the colums will fill with NA if is not present ? something like:

           elem1 elem2 elem3
sample-01   E7   E1   NA
sample-02   E7   E1   NA
sample-03   E4   E8   NA
sample-04   E8   E3   NA
sample-05   E9   NA   E2
sample-06   E8   NA   E2
sample-07   NA   NA   E7
sample-08   NA   NA   E2

thanks so much !!!

CodePudding user response:

mylist %>%
   map(rownames_to_column, "sample") %>%
   reduce(full_join, "sample")

     sample elem1 elem2 elem3
1 sample-01    E7    E1  <NA>
2 sample-02    E7    E1  <NA>
3 sample-03    E4    E8  <NA>
4 sample-04    E8    E3  <NA>
5 sample-05    E9  <NA>    E2
6 sample-06    E8  <NA>    E2
7 sample-07  <NA>  <NA>    E7
8 sample-08  <NA>  <NA>    E2

CodePudding user response:

dplyr::bind_rows() with some postprocessing is an example of a tidy solution:

df_list %>%
  map(rownames_to_column, "sample") %>%
  bind_rows() %>%
  group_by(sample) %>%
  summarise(across(everything(), ~first(na.omit(.))))

# # A tibble: 8 × 4
#   sample    elem1 elem2 elem3
#   <chr>     <chr> <chr> <chr>
# 1 sample-01 E7    E1    NA   
# 2 sample-02 E7    E1    NA   
# 3 sample-03 E4    E8    NA   
# 4 sample-04 E8    E3    NA   
# 5 sample-05 E9    NA    E2   
# 6 sample-06 E8    NA    E2   
# 7 sample-07 NA    NA    E7   
# 8 sample-08 NA    NA    E2   

Data:

df_list <- list(elem1 = read.table(text = "         elem1
sample-01   E7
sample-02   E7
sample-03   E4
sample-04   E8
sample-05   E9
sample-06   E8", header = TRUE),

elem2 = read.table(text = "     elem2
sample-01   E1
sample-02   E1
sample-03   E8
sample-04   E3", header = TRUE),

elem3 = read.table(text = "      elem3
sample-05   E2
sample-06   E2
sample-07   E7
sample-08  E2", header = TRUE))
  • Related