[R] How to stack the columns of matrix

Agustin Lobo alobo at paleo.ija.csic.es
Wed Jun 12 17:47:54 CEST 2002


If you have no idea of the max. number of cols, then
a <- NULL
and
each time you have a new vector to add:
a <- cbind(a,newvector) 

If you can have some idea of the maximum number of cols., 
it seems that it's more efficient for the memory to create
a <- matrix(0,nrow=nr, ncol=maxcol) 

and then

newcolnumber <- newcolnumber + 1 
#having newcolnumber <- 0 at the begining of the function
a[,newcolnumber] <- newvector

A the end, keep only those cols that have been used
(the last value of newcolnumber):
a[,1:newcolnumber]

Agus


On Wed, 12 Jun 2002, Dr. Peter Schlattmann wrote:

> Dear all,
> 
> I need to stack the columns of a matrix with a variable number of columns.
> I am aware of 
> 
> new<-c(a[,1],a[,2])
> 
> if I have a matrix 'a' with e.g. two columns. For a simulation study I
> need to stack 
> the matrix with a variable number of columns. Is there a way to do this?
> 
> Many thanks in advance
> Peter
> 
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list