How to transform matrices based on object names?

I have several matrices that I would like rbind in a single rbind . They are product objects of various functions, and they are of a general nature in their names.

What I want to do is say R to search for all objects with this shared pattern, and then rbind them.

Assuming that 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" 

I can't figure out how to tell rbind use this as an argument. Basically, I would do something that gives the same matrix as rbind(commonname.N1, commonname.N2, commonname.M1) in this example.

I tried things in line

 mats<-toString(mats) rbind(mats2) 

but it just creates a matrix with different objects as names.

A similar question was asked here , but:

 mats<-as.list(mats) do.call(what=rbind, args=as.list(mats)) 

not doing this job.

Sorry if there is something basic, I'm missing something, but I can't figure it out, and I'm relatively new to R.

+4
source share
1 answer

Use mget :

 do.call(rbind,mget(mats)) 
+7
source

Source: https://habr.com/ru/post/1495939/


All Articles