Generic character

Hellow i would like to bind multiple data together i have 100 data_frames named df1 to df100
and i dont want to make rbind(df1,df2,df3 etc)
Do you know an easy way to make it?

It would be best to build a list containing the data frames as they are created and then use the bind_rows() function of dplyr. If that cannot be done, you can load the data frames into a list afterwards, as shown below.

library(dplyr, warn.conflicts = FALSE)
#make 3 data frames
df1 <- data.frame(A = 1:3)
df2 <- data.frame(A = 4:6)
df3 <- data.frame(A = 7:9)

DF_names <- paste0("df", 1:3)

#load the data frames into a list
#best to do this when the data frames are made
DFs <- vector(mode = "list", length = length(DF_names))
names(DFs) <- DF_names
for(NM in DF_names) {
  DFs[[NM]] <- get(NM)
}

AllData <- bind_rows(DFs, .id = "OrigDF")
AllData
#>   OrigDF A
#> 1    df1 1
#> 2    df1 2
#> 3    df1 3
#> 4    df2 4
#> 5    df2 5
#> 6    df2 6
#> 7    df3 7
#> 8    df3 8
#> 9    df3 9

Created on 2020-07-26 by the reprex package (v0.3.0)

2 Likes

okay i think that's correct thanksss a lot

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.