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 [[alternative HTML version deleted]]
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
Hello, Maybe using ?Reduce: Zlist <- c(mat1, mat2, mat3) Z <- Reduce(cbind2, Zlist) Ztmp <- cbind2(mat1, mat2) Z2 <- cbind2(Ztmp, mat3) identical(Z, Z2) # TRUE Also, I prefer list(mat1, mat2, mat3), not c(). Hope this helps, Rui Barradas Em 12-11-2013 17:20, Doran, Harold escreveu:> 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 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >