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

From: Kristel Joossens <kristel.joossens_at_econ.kuleuven.ac.be>
Date: Mon 05 Dec 2005 - 04:39:35 EST

I think I know what you would like to have.

Sometyhing you can do

R> res<-vector("list",5)
R> for (i in 1:5)
R> {...
R>   res[[i]]=my_code(i)
R> }

As an easy example:

R> res<-vector("list",5)
R> for (i in 1:5){res[[i]]=1:i}
R> ex

[[1]]
[1] 1

[[2]]
[1] 1 2

[[3]]
[1] 1 2 3

[[4]]
[1] 1 2 3 4

[[5]]
[1] 1 2 3 4 5

Best regards,
Kristel

-- 
__________________________________________
Kristel Joossens        Ph.D. Student
Research Center ORSTAT  K.U. Leuven
Naamsestraat 69         Tel: +32 16 326929
3000 Leuven, Belgium    Fax: +32 16 326732
E-mail:  Kristel.Joossens@econ.kuleuven.be
http://www.econ.kuleuven.be/public/ndbae49




Serguei Kaniovski wrote:

> Hi, thanks for your reply. What I have is a large chunk of code
> "my_code" that is controlled by a single FOR-loop. The code computes a
> single value my_code(i) depending on the loop counter i. What I would
> like to do is to organize the input and the output into a table so that
> it can be e.g. plotted, for example in this format
>
> input_i i=1 i=2 i=3 ....
> my_code(1) my_code(2) my_code(3)
> ...
>
> where my_code(1) is the output of a single iteration, itself a column
> vector of variable size N(i)
>
> Serguei
>
> Kristel Joossens schrieb:
>
>> 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 >> >> >> >> First of all you can easily avoid for-loops in such examples. >> You can better work with matrices, so just do >> R> i <- 1:5 >> R> x <- i^2 >> R> y <- i^3 >> >> Second, maybe very interesting, if you want to look somethimg up and >> do not know the R-command. You can always try with RSiteSearch("..."). >> >> I'm not sure what you mean with ``construct a data.frame in a for >> loop''. As far as I know you can better first do the loop and >> construct at the end the data.frame. Anyway, to create a data.frame, >> you can e.g. do >> R> resdf <- as.data.frame(cbind(i,x,y)) >> >> To create 3D-plot there are seeral possibilities. >> Try ?persp (for surfaces) >> or you can download the scatterplot3d package available on CRAN (for >> points and lines). >> >> Hopefully this helps, >> Good luck, >> Kristel Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm ______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Mon Dec 05 05:24:54 2005

This archive was generated by hypermail 2.1.8 : Mon 05 Dec 2005 - 09:25:00 EST