Search code examples
rdataframedplyrinner-jointibble

join list of data frames in one dataframe


I have the following list of tibbles:

list(structure(list(comp_levels = c("C1-C", "C2-C", "C3-C", "C4-C", 
"C5-C"), pval = c(0.766639095524166, 0.937892422573886, 0.771590751986251, 
0.0650891911214325, 0.998166537304179), sig_t0 = c(NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_)), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    comp_levels = c("C1-C", "C2-C", "C3-C", "C4-C", "C5-C"), 
    pval = c(0.993499501490807, 0.19139507384505, 0.0284772203585322, 
    0.00101491942365795, 9.38246273962662e-06), sig_t1 = c(NA, 
    NA, "*", "*", "*")), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(comp_levels = c("C1-C", 
"C2-C", "C3-C", "C4-C", "C5-C"), pval = c(0.997883600614787, 
0.864072764364873, 0.0190462395398602, 0.00110839404339469, 1.32759553026496e-05
), sig_REC1 = c(NA, NA, "*", "*", "*")), row.names = c(NA, -5L
), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    comp_levels = c("C1-C", "C2-C", "C3-C", "C4-C", "C5-C"), 
    pval = c(0.994104631665907, 0.831335063966987, 0.344816431966943, 
    0.00787226030158372, 3.98545386042226e-05), sig_REC2 = c(NA, 
    NA, NA, "*", "*")), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(comp_levels = c("C1-C", 
"C2-C", "C3-C", "C4-C", "C5-C"), pval = c(0.999646089251718, 
0.627948789074042, 0.619398267779987, 0.00504065836957468, 5.82357327145733e-05
), sig_REC4 = c(NA, NA, NA, "*", "*")), row.names = c(NA, -5L
), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    comp_levels = c("C1-C", "C2-C", "C3-C", "C4-C", "C5-C"), 
    pval = c(0.998802469995545, 0.927532603864203, 0.999999998703932, 
    0.105513456174707, 0.00667866346900203), sig_REC8 = c(NA, 
    NA, NA, NA, "*")), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(comp_levels = c("C1-C", 
"C2-C", "C3-C", "C4-C", "C5-C"), pval = c(0.999987168448056, 
0.890620355596417, 0.997461809118391, 0.134369579871575, 0.0443384965681386
), sig_REC11 = c(NA, NA, NA, NA, "*")), row.names = c(NA, -5L
), class = c("tbl_df", "tbl", "data.frame")))

All the tibbles in the list have the same column names. I would to join all the tibbles by column comp_name, keeping just this column and not the column pval in one tibble. How can I do this?


Solution

  • We could use reduce from purrr package:

    "reduce() is an operation that combines the elements of a vector into a single value. The combination is driven by .f, a binary function that takes two values and returns a single value: reducing f over 1:3 computes the value f(f(1, 2), 3)." ?reduce

    library(dplyr)
    library(purrr)
    
    mylist %>% 
      reduce(left_join, by = "comp_levels") %>% 
      select(-starts_with("pval"))
    
      comp_levels sig_t0 sig_t1 sig_REC1 sig_REC2 sig_REC4 sig_REC8 sig_REC11
      <chr>       <chr>  <chr>  <chr>    <chr>    <chr>    <chr>    <chr>    
    1 C1-C        NA     NA     NA       NA       NA       NA       NA       
    2 C2-C        NA     NA     NA       NA       NA       NA       NA       
    3 C3-C        NA     *      *        NA       NA       NA       NA       
    4 C4-C        NA     *      *        *        *        NA       NA       
    5 C5-C        NA     *      *        *        *        *        *