[R] matrix with fix number columns but variable number rows

Agustin Lobo alobo at ija.csic.es
Tue Mar 19 10:24:39 CET 2002


If the vectors are created within a loop,

matriz <- NULL
for(...){
 ...
 matriz <- cbind(matriz,vector)
}
matriz

but I think that it's better to define matriz with a large
number of cols and avoid growing the matrix within the
loop:

matriz <- matrix(NULL,nrow=numlineas,ncol=maxnumcols)
col <- 0
for(...){ 
 col <- col+1
 ... 
 matriz[,col] <- vector
}
matriz <- matriz[,1:col] #keep only the used cols
matriz

..and even better if you can avoid the loop, R offers
many opportunities for that.



Dr. Agustin Lobo
Instituto de Ciencias de la Tierra (CSIC)
Lluis Sole Sabaris s/n
08028 Barcelona SPAIN
tel 34 93409 5410
fax 34 93411 0012
alobo at ija.csic.es


On Mon, 18 Mar 2002, Francisco J Molina wrote:

> I have to store a number of vectors of the same length. I know the
> length but I do not know the number of vectors.
> 
> How can I store them as they are created (they are created one by one)?
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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