[R] Re : Save result in a For Loop

bogdan romocea br44114 at yahoo.com
Tue Dec 14 21:44:52 CET 2004

Not sure if it's the best way, but you could do it this way:
all.results <- vector(mode="numeric")
for (i in 1:100)
	this.run <- ...
	all.results <- c(all.results,this.run)
At this point all.results contains the values of this.run from the
whole loop. If this.run is not a vector/number but a data frame look
at rbind/cbind.

Or, create a vector/matrix first and then populate it from the for
all.results <- vector()/matrix()/data.frame()
for (i in 1:100)
  for(j ...)
	all.results[i] <- this.run  ,OR
	all.results[i,] <- this.run  , OR
	all.results[i,j] <- this.run


-----Original Message-----
From: ssim at lic.co.nz [mailto:ssim at lic.co.nz]
Sent: Tuesday, December 14, 2004 2:44 PM
To: r-help at stat.math.ethz.ch
Subject: [R] Re : Save result in a For Loop


I have been struggling to save the result from the FOR loop. What is
best way to do it, as I need the result to merge with another dataset
further analysis ?

for (dd in ((M-10):M)){
+ dist<-(32-dd)
+ r<-1/2*(1-exp(-2*dist/100))
+ map<-c(dd,round(r,4))
+ print(map)
+ next
+ }

Thanks. Stella
This message, including attachments, is confidential. If\ yo...{{dropped}}

More information about the R-help mailing list