I have several matrices that I would like to rbind
in a single summary one. They are objects product of different functions and they have share a pattern in their names.
What I want to do is to tell R to look for all the objects with that common pattern and then rbind
them.
Assuming these matrices exist:
commonname.N1<-matrix(nrow=2,ncol=3)
commonname.N2<-matrix(nrow=2,ncol=3)
commonname.M1<-matrix(nrow=2,ncol=3)
I tried something like this to get them:
mats<-grep(x= ls(pos=1), pattern="commonname.", value=TRUE)
mats
[1] "commonname.N1" "commonname.N2" "commonname.M1"
What I can't figure out is how to tell rbind
to use that as argument. Basically I would something that gives the same matrix than what rbind(commonname.N1, commonname.N2, commonname.M1)
would do in this example.
I have tried things on the line of
mats<-toString(mats)
rbind(mats2)
but that just creates a matrix with the different objects as names.
A similar question was asked here, but:
mats<-as.list(mats)
do.call(what=rbind, args=as.list(mats))
doesn't do the job.
Sorry if there is something basic I'm missing somewhere, but I can't figure it out and I'm relatively new to R.
Use mget
:
do.call(rbind,mget(mats))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With