0

In R, I currently have 100 dataframes, named df.1, ...,df.100. I would like to be able to rbind them but it is costly to write out:

rbind(df.1, df.2, etc)

So, I have tried:

rbind(eval(as.symbol(paste0("df.",1:84, collapse = ", "))))

However, this returns errors. Does anyone know how I can make the dataframes usable? thanks.

user321627
  • 2,350
  • 4
  • 20
  • 43
  • 3
    If you have any control over how these 100 dataframes are created in the first place, it's probably much easier to read them into a `list()` in the first place so you can access them using `df_list[["df1"]]`. – Marius Jun 18 '18 at 01:19
  • 1
    I'd strongly recommend reading [How do I make a list of data frames](https://stackoverflow.com/a/24376207/903061). – Gregor Thomas Jun 18 '18 at 01:33

2 Answers2

1

You can rbind them one at a time in a loop.

df.1 = iris
df.2 = iris
df.3 = iris

DF = df.1
for(i in 2:3) {
    DF = rbind(DF, eval(as.symbol(paste("df", i, sep=".")))) }
G5W
  • 36,531
  • 10
  • 47
  • 80
1

Using mget and then do.call or dplyr's bind_rows should work.

df.1 = iris[1:20,]
df.2 = iris[21:50,]

do.call("rbind",mget(paste0("df.",1:2)))

library(dplyr)
bind_rows(mget(paste0("df.",1:2)))
Jul
  • 1,129
  • 6
  • 12
  • It seems that the `do.call` function gives rownames that do not match. Is there a reason for that? – user321627 Jun 18 '18 at 01:54
  • 2
    @user321627 - rownames that don't match what? They are equivalent to `datasetname.rownumber`, presumably so you can match back to the original datasets if need be. – thelatemail Jun 18 '18 at 01:57