Skip to content

combining vectors to matrices or data frames

3 messages · Jan M. Wiener, Renaud Lancelot, Erich Neuwirth

#
hi,

during a for(i in 1:xx]) loop I always newly calculate a vector 
(e.g. tmp<-c(a,b,c,d,e) )
now i need that vector to be attached at the bootom of a matrix (or
data.frame).


e.g.

m<-matrix()

for(i in 1:5]){
	#some calculations for a,b,c,d,e
	a<- ... b<- ... c<- .. ....

	tmp<-c(a,b,c,d,e)

	??? now I need to attach this tmp to the matrix m ???
}

i couldn't get rbind or append to work properly 

thanks for advice,
jan
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
use list and do.call:

MyList <- list()

for(i in 1:5]){
  #some calculations for a,b,c,d,e
  a<- ... b<- ... c<- .. ....
  MyList[[i]] <- c(a,b,c,d,e)
  }

MyData <- do.call("rbind", MyList)

will work fro relatively small ( < 1000, say) values of i. Otherwise,
other solutions will be quicker.

Best,

Renaud
Jan Malte Wiener wrote:

  
    
#
a brute force solution would be

tmp<-c()
for(i in 1:5]){
	#some calculations for a,b,c,d,e
	a<- ... b<- ... c<- .. ....

	tmp<-c(tmp,c(a,b,c,d,e))}

m<-t(matrix(tmp,5,5))
Jan Malte Wiener wrote: