Search code examples
rmergerbind

R- How to merge/bind several dataframes considering that some of the called dataframes might not exist?


I have to bind/merge dataframes in an iterative process in R. In occasions, some of the dataframes might not exist for reasons I will not explain here. I would like to know how I could bind dataframes even when calling some dataframe that does not exist. As an example, I have these dataframes:

df.R_H <- data.frame(A=c(0,4,5,6,7),
                  B=c(3,9,4,5,8),
                  C=c(1,2,4,2,6))

df.B_C <- data.frame(A=c(0,4,5,6,7),
                  B=c(3,9,4,5,8),
                  C=c(1,2,4,2,6))

df.G_H <- data.frame(A=c(0,4,5,6,7),
                  B=c(3,9,4,5,8),
                  C=c(1,2,4,2,6))

However, in my code I have this:

df5 <- rbind(df.R_H,df.B_C,df.G_H,df.R_C)
df5

How can I bind dataframes although df.R_C does not exist? I can not check individually which dataframes exists or not since I have hundreds of situations like this and I want to do it automatically even when some dataframe does not exist.


Solution

  • Another possible solution:

    library(tidyverse)
    
    df.R_H <- data.frame(A=c(0,4,5,6,7),
                         B=c(3,9,4,5,8),
                         C=c(1,2,4,2,6))
    
    df.B_C <- data.frame(A=c(0,4,5,6,7),
                         B=c(3,9,4,5,8),
                         C=c(1,2,4,2,6))
    
    df.G_H <- data.frame(A=c(0,4,5,6,7),
                         B=c(3,9,4,5,8),
                         C=c(1,2,4,2,6))
    
    map_dfr(list("df.R_H","df.B_C","df.G_H","df.R_C"), ~ if (exists(.x)) {get(.x)})
    
    #>    A B C
    #> 1  0 3 1
    #> 2  4 9 2
    #> 3  5 4 4
    #> 4  6 5 2
    #> 5  7 8 6
    #> 6  0 3 1
    #> 7  4 9 2
    #> 8  5 4 4
    #> 9  6 5 2
    #> 10 7 8 6
    #> 11 0 3 1
    #> 12 4 9 2
    #> 13 5 4 4
    #> 14 6 5 2
    #> 15 7 8 6