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
--
___________________________________________________________________
??sterreichisches Institut f??r Wirtschaftsforschung (WIFO)
Name: Serguei Kaniovski Postadresse: Postfach 91
Tel.: +43-1-7982601-231 A-1103 Wien
Fax : +43-1-7989386 Standort: Arsenal Objekt 20
Mail: Serguei.Kaniovski at wifo.ac.at A-1030 Wien
http://www.wifo.ac.at/
>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 > x<-numeric(5) > y<-numeric(5) > for(i in 1:5)+ { + x[i]<-i^2 + y[i]<-i^3 + }> da<-data.frame(index=1:5,x=x,y=y) > daindex x y 1 1 1 1 2 2 4 8 3 3 9 27 4 4 16 64 5 5 25 125 these can get the data frame.>and a 3D plot of (i,x(i),y(i)), i.e. for >each iteration > >Thanks, >Serguei Kaniovski >-- >___________________________________________________________________ > >佒sterreichisches Institut f侟r Wirtschaftsforschung (WIFO) > >Name: Serguei Kaniovski Postadresse: Postfach 91 >Tel.: +43-1-7982601-231 A-1103 Wien >Fax : +43-1-7989386 Standort: Arsenal Objekt 20 > >Mail: Serguei.Kaniovski at wifo.ac.at A-1030 Wien > >http://www.wifo.ac.at/ > >______________________________________________ >R-help at 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= = = = = = = = = = = = = = = = = = = 2005-12-04 ------ Deparment of Sociology Fudan University My new mail addres is ronggui.huang at gmail.com Blog:http://sociology.yculblog.com
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 KaniovskiIf thats all you really need: data<- t(sapply(1:5, function(i){ return(c(i,i^2,i^3))})) scatterplot3d(data) 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. Regards, Ido Tamir
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 iterationFirst 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
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 at 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