[R] cbind2() in Matrix

arun smartpink111 at yahoo.com
Tue Nov 12 18:51:32 CET 2013


Hi,
Try:

Reduce(function(...) cbind2(...),Zlist)

A.K.


Suppose I have three matrices, such as the following: 

mat1 <- Matrix(rnorm(9), 3) 
mat2 <- Matrix(rnorm(9), 3) 
mat3 <- Matrix(rnorm(9), 3) 

I now need to column bind these and I could do the following if 
there were only two of those matrices because cbind2() has an x and y 
argument 

Zlist <- c(mat1, mat2) 
Z <- do.call(cbind2, Zlist) 

The following would not work as noted in the help page for cbind2() and I don't think I want to activate cbind() here. 

Zlist <- c(mat1, mat2, mat3) 
Z <- do.call(cbind2, Zlist) 

So, the object I would want in the end would be 
Ztmp <- cbind2(mat1, mat2) 
Z <- cbind2(Ztmp, mat3) 

I never have a large number of these things to combine, so I have solved the problem with a simple loop over the list. 

I'm curious though if there is a better (and perhaps) more reliable way to do this? 

Thanks, 
Harold



More information about the R-help mailing list