[R] cbind2() in Matrix

Martin Maechler maechler at stat.math.ethz.ch
Fri Nov 15 18:59:06 CET 2013


>>>>> "RB" == Rui Barradas <ruipbarradas at sapo.pt>
>>>>>     on Tue, 12 Nov 2013 18:52:31 +0000 writes:

    RB> Hello,

    RB> Maybe using ?Reduce:

    RB> Zlist <- c(mat1, mat2, mat3)
    RB> Z <- Reduce(cbind2, Zlist)

    RB> Ztmp <- cbind2(mat1, mat2)
    RB> Z2 <- cbind2(Ztmp, mat3)

    RB> identical(Z, Z2)  # TRUE

    RB> Also, I prefer list(mat1, mat2, mat3), not c().
(me too)

    RB> Hope this helps,
    RB> Rui Barradas

Well... I'm a bit disappointed that nobody took up and made the
matter a bit more clear :

1) cbind2() is not from package Matrix, but from standard R,
   package 'methods'.
   It is true (and necessary!) that Matrix defines many (S4)
   methods for cbind2(), indeed.

2) The help page for cbind2() {and rbind2()} mentions that their
   main use is to be used as building blocks for more than two
   arguments, and then mentions the possibility of
     methods:::bind_activation(TRUE)
   which makes  cbind() and rbind()  being "S4" aware and using
   cbind2() and rbind2() for column and row binding.

   The help page then has a "link" to  cBind() and rBind()  in
   the Matrix package, and these actually *are* cbind and rbind
   version that work with an arbitrary number of matrices
   (Matrix package or other; it will work whenever cbind2() /
   rbind2() methods are defined).

So, where as you can use cbind with  Reduce()   and that is nice functional
programming {not available at the time  cBind / rBind
were created},
actually the whole idea was that you would simply
use
	cBind(mat1, mat2, mat3)

Maybe we have to review this matter, or at least add something
to the help pages.

Martin Maechler, 
ETH Zurich


    RB> 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.
    >> 

    RB> ______________________________________________
    RB> R-help at r-project.org mailing list
    RB> https://stat.ethz.ch/mailman/listinfo/r-help
    RB> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    RB> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list