简体   繁体   中英

How to rbind matrices based on objects names?

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.

使用mget

do.call(rbind,mget(mats))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM