Search code examples
rvectorcbind

Identify and cbind multiple vectors based on vector name


I have 100 numeric vectors named sim1 to sim100 in my workspace, all of the same length (18). I'm trying to find a way to identify them and cbind them to create a data frame of 18 rows and 100 columns. I can easily create a character vector of length 100 that contains the names of the vectors:

myvector<-ls()
myvector<-[grep("sim",myvector)]

..but I'm stuck on how to create a list of the objects themselves that I assume I could then use with do.call. Any suggestions please?


Solution

  • You can try

     do.call(cbind.data.frame, mget(paste0('sim', 1:100)))
    

    Or as @Frank mentioned in the comments

     data.frame(mget(paste0('sim', 1:100)))