# [R] Re : Save result in a For Loop

Liaw, Andy andy_liaw at merck.com
Wed Dec 15 01:41:00 CET 2004

```Is there anything wrong with vectorized calculation?  E.g.,

dd <- (M-10):M
r <- 0.5 * (1 - exp(-2 * (32 - dd) / 100))
round(r, 4)

Andy

> From: bogdan romocea
>
> 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
> loop:
> 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
> 	}
>
> HTH,
> b.
>
>
> -----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
>
>
> Hiya,
>
> I have been struggling to save the result from the FOR loop. What is
> the
> best way to do it, as I need the result to merge with another dataset
> for
> 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\\...{{dropped}}

```

More information about the R-help mailing list