R Paste List to Bind

Viewed 109
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..

1 Answers

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)))
Related