2
data1 = data.frame("time" = c(1:10))
data2 = data.frame("time" = c(11:20))
data3 = data.frame("time" = c(21:30))
data4 = data.frame("time" = c(31:40))

rbind(data1, data2, data3, data4)

rbind(paste("'","data","'",1:4,sep=","))

I want to bind together a whole bunch of data frames but instead of spelling out all of them want to use paste functions. Here in my simple example you will see it doesn't work as desired but when I spell out the dataframes it works..

Sathish
  • 12,453
  • 3
  • 41
  • 59
bvowe
  • 3,004
  • 3
  • 16
  • 33
  • Does this answer your question? [rbind data frames based on a common pattern in data frame name](https://stackoverflow.com/questions/41811675/rbind-data-frames-based-on-a-common-pattern-in-data-frame-name) – camille Apr 03 '20 at 22:13

1 Answers1

3

We can use mget on the pasted strings to return the values of the object names in a list and then rbind the elements with do.call

`row.names<-`(do.call(rbind, mget(paste0('data', 1:4))), NULL)

Or use pattern in ls

do.call(rbind, mget(ls(pattern = '^data\\d+$')))

With data.table, it would be rbindlist

library(data.table)
rbindlist(mget(paste0('data', 1:4)))
akrun
  • 874,273
  • 37
  • 540
  • 662