4

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?

oguz ismail
  • 1
  • 16
  • 47
  • 69
Ian Fisher
  • 43
  • 2
  • Similar, maybe a dupe: http://stackoverflow.com/questions/13105888/in-r-how-to-easily-combine-many-vectors-into-a-dataframe – Frank May 26 '15 at 15:36

1 Answers1

2

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)))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    Is `data.frame` the same as `cbind.data.frame` here, effectively? Actually, I guess `as.data.frame(mget(...))` also works. – Frank May 26 '15 at 15:29
  • @Frank It is the same, but I started with `cbind`, and then changed to `cbind.data.frame`. Also, `data.frame(mget(...))` – akrun May 26 '15 at 15:31
  • 1
    Thanks for these answers. Really useful. – Ian Fisher May 26 '15 at 15:44