[R] Construct a data.frame in a FOR-loop

Ido M. Tamir tamir at imp.univie.ac.at
Sun Dec 4 17:07:14 CET 2005

Serguei Kaniovski wrote:

> Say I have a FOR-loop for computing powers (just a trivial example)
> for(i in 1:5)
> {
> x<-i^2
> y<-i^3
> }
> How can I create a data.frame and a 3D plot of (i,x(i),y(i)), i.e. for
> each iteration
> Thanks,
> Serguei Kaniovski

If thats all you really need:
data<- t(sapply(1:5, function(i){ return(c(i,i^2,i^3))}))

In general you don't plot something in R for each iteration.
You rather pass the whole result to the plot function.
But there are plotting function which accept formulas.

Ido Tamir

More information about the R-help mailing list