loop

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

loop

gynmeerut

Dear All,

I have to use loop over an array  so I am using following procedure
 
count<-1
 repeat{
 count<-count + 1
 c(g[count],1:i[count]) ->qw
 if(count>5)break
 }

as  a result qw is
[1]  0.9643836  1.0000000  2.0000000  3.0000000  4.0000000  5.0000000
 [7]  6.0000000  7.0000000  8.0000000  9.0000000 10.0000000 11.0000000
[13] 12.0000000 13.0000000 14.0000000 15.0000000 16.0000000 17.0000000
[19] 18.0000000 19.0000000 20.0000000 21.0000000 22.0000000 23.0000000
[25] 24.0000000 25.0000000 26.0000000 27.0000000 28.0000000 29.0000000
[31] 30.0000000 31.0000000 32.0000000 33.0000000 34.0000000 35.0000000
[37] 36.0000000 37.0000000 38.0000000 39.0000000

which is according to the last value of  count i.e 6. What is the problem in error
 

but I expect the result as

 [1]  0.8328767  0.2410959  0.5315068  0.1424658  0.2520548  0.9643836
 [7]  6.0000000  7.0000000  8.0000000  0.2410959  1.0000000  2.0000000
[13]  3.0000000  4.0000000  5.0000000  0.5315068  1.0000000  2.0000000
[19]  3.0000000  4.0000000  5.0000000  6.0000000  7.0000000  8.0000000
[25]  9.0000000 10.0000000 11.0000000 12.0000000 13.0000000  0.1424658
[31]  1.0000000  0.0000000  0.2520548  1.0000000  2.0000000  3.0000000
[37]  4.0000000  5.0000000  6.0000000  7.0000000  8.0000000  9.0000000
[43] 10.0000000 11.0000000 12.0000000 13.0000000 14.0000000 15.0000000
[49] 16.0000000 17.0000000 18.0000000 19.0000000 20.0000000 21.0000000
[55] 22.0000000 23.0000000  0.9643836  1.0000000  2.0000000  3.0000000
[61]  4.0000000  5.0000000  6.0000000  7.0000000  8.0000000  9.0000000
[67] 10.0000000 11.0000000 12.0000000 13.0000000 14.0000000 15.0000000
[73] 16.0000000 17.0000000 18.0000000 19.0000000 20.0000000 21.0000000
[79] 22.0000000 23.0000000 24.0000000 25.0000000 26.0000000 27.0000000
[85] 28.0000000 29.0000000 30.0000000 31.0000000 32.0000000 33.0000000
[91] 34.0000000 35.0000000 36.0000000 37.0000000 38.0000000 39.0000000

which is combination of 6 vectors of lengths {9,6,14,3,24,40).


Please tell me the error or alternate procedure for looping.

regards,

GS

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: loop

Duncan Murdoch
On 12/29/2005 7:41 AM, gynmeerut wrote:

> Dear All,
>
> I have to use loop over an array  so I am using following procedure
>  
> count<-1
>  repeat{
>  count<-count + 1
>  c(g[count],1:i[count]) ->qw
>  if(count>5)break
>  }

We can't reproduce this, as we don't have g or i.  But the general
advice in a case like this is to simulate the loop by hand:  write down
what is in each of the variables, and walk through the loop.

I suspect your problem is in initializing count improperly, or in
putting the test in the wrong place.

Duncan Murdoch

>
> as  a result qw is
> [1]  0.9643836  1.0000000  2.0000000  3.0000000  4.0000000  5.0000000
>  [7]  6.0000000  7.0000000  8.0000000  9.0000000 10.0000000 11.0000000
> [13] 12.0000000 13.0000000 14.0000000 15.0000000 16.0000000 17.0000000
> [19] 18.0000000 19.0000000 20.0000000 21.0000000 22.0000000 23.0000000
> [25] 24.0000000 25.0000000 26.0000000 27.0000000 28.0000000 29.0000000
> [31] 30.0000000 31.0000000 32.0000000 33.0000000 34.0000000 35.0000000
> [37] 36.0000000 37.0000000 38.0000000 39.0000000
>
> which is according to the last value of  count i.e 6. What is the problem in error
>  
>
> but I expect the result as
>
>  [1]  0.8328767  0.2410959  0.5315068  0.1424658  0.2520548  0.9643836
>  [7]  6.0000000  7.0000000  8.0000000  0.2410959  1.0000000  2.0000000
> [13]  3.0000000  4.0000000  5.0000000  0.5315068  1.0000000  2.0000000
> [19]  3.0000000  4.0000000  5.0000000  6.0000000  7.0000000  8.0000000
> [25]  9.0000000 10.0000000 11.0000000 12.0000000 13.0000000  0.1424658
> [31]  1.0000000  0.0000000  0.2520548  1.0000000  2.0000000  3.0000000
> [37]  4.0000000  5.0000000  6.0000000  7.0000000  8.0000000  9.0000000
> [43] 10.0000000 11.0000000 12.0000000 13.0000000 14.0000000 15.0000000
> [49] 16.0000000 17.0000000 18.0000000 19.0000000 20.0000000 21.0000000
> [55] 22.0000000 23.0000000  0.9643836  1.0000000  2.0000000  3.0000000
> [61]  4.0000000  5.0000000  6.0000000  7.0000000  8.0000000  9.0000000
> [67] 10.0000000 11.0000000 12.0000000 13.0000000 14.0000000 15.0000000
> [73] 16.0000000 17.0000000 18.0000000 19.0000000 20.0000000 21.0000000
> [79] 22.0000000 23.0000000 24.0000000 25.0000000 26.0000000 27.0000000
> [85] 28.0000000 29.0000000 30.0000000 31.0000000 32.0000000 33.0000000
> [91] 34.0000000 35.0000000 36.0000000 37.0000000 38.0000000 39.0000000
>
> which is combination of 6 vectors of lengths {9,6,14,3,24,40).
>
>
> Please tell me the error or alternate procedure for looping.
>
> regards,
>
> GS
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: loop

Gichangi, Anthony
In reply to this post by gynmeerut
Your loop will store results of count =6 because
every time the loop executes the results are put in
qw so you replace the previous results.  On the other
hand the results of count=1 is not extracted you basicaly
start at 2.

My advice is you initialize qw before the loop and stack the results
one after the other e.g.

 qw <-NULL
 count<-1
  repeat{
 qw<-c(qw,  c(g[count],1:i[count]) )
  count<-count + 1
  if(count>5) break
 }

Kind regards
Anthony Gichangi

----- Original Message -----
From: "gynmeerut" <[hidden email]>
To: <[hidden email]>
Sent: Thursday, December 29, 2005 1:41 PM
Subject: [R] loop


>
> Dear All,
>
> I have to use loop over an array  so I am using following procedure
>
> count<-1
> repeat{
> count<-count + 1
> c(g[count],1:i[count]) ->qw
> if(count>5)break
> }
>
> as  a result qw is
> [1]  0.9643836  1.0000000  2.0000000  3.0000000  4.0000000  5.0000000
> [7]  6.0000000  7.0000000  8.0000000  9.0000000 10.0000000 11.0000000
> [13] 12.0000000 13.0000000 14.0000000 15.0000000 16.0000000 17.0000000
> [19] 18.0000000 19.0000000 20.0000000 21.0000000 22.0000000 23.0000000
> [25] 24.0000000 25.0000000 26.0000000 27.0000000 28.0000000 29.0000000
> [31] 30.0000000 31.0000000 32.0000000 33.0000000 34.0000000 35.0000000
> [37] 36.0000000 37.0000000 38.0000000 39.0000000
>
> which is according to the last value of  count i.e 6. What is the problem
> in error
>
>
> but I expect the result as
>
> [1]  0.8328767  0.2410959  0.5315068  0.1424658  0.2520548  0.9643836
> [7]  6.0000000  7.0000000  8.0000000  0.2410959  1.0000000  2.0000000
> [13]  3.0000000  4.0000000  5.0000000  0.5315068  1.0000000  2.0000000
> [19]  3.0000000  4.0000000  5.0000000  6.0000000  7.0000000  8.0000000
> [25]  9.0000000 10.0000000 11.0000000 12.0000000 13.0000000  0.1424658
> [31]  1.0000000  0.0000000  0.2520548  1.0000000  2.0000000  3.0000000
> [37]  4.0000000  5.0000000  6.0000000  7.0000000  8.0000000  9.0000000
> [43] 10.0000000 11.0000000 12.0000000 13.0000000 14.0000000 15.0000000
> [49] 16.0000000 17.0000000 18.0000000 19.0000000 20.0000000 21.0000000
> [55] 22.0000000 23.0000000  0.9643836  1.0000000  2.0000000  3.0000000
> [61]  4.0000000  5.0000000  6.0000000  7.0000000  8.0000000  9.0000000
> [67] 10.0000000 11.0000000 12.0000000 13.0000000 14.0000000 15.0000000
> [73] 16.0000000 17.0000000 18.0000000 19.0000000 20.0000000 21.0000000
> [79] 22.0000000 23.0000000 24.0000000 25.0000000 26.0000000 27.0000000
> [85] 28.0000000 29.0000000 30.0000000 31.0000000 32.0000000 33.0000000
> [91] 34.0000000 35.0000000 36.0000000 37.0000000 38.0000000 39.0000000
>
> which is combination of 6 vectors of lengths {9,6,14,3,24,40).
>
>
> Please tell me the error or alternate procedure for looping.
>
> regards,
>
> GS
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: loop

Peter Dalgaard
In reply to this post by Duncan Murdoch
Duncan Murdoch <[hidden email]> writes:

> On 12/29/2005 7:41 AM, gynmeerut wrote:
> > Dear All,
> >
> > I have to use loop over an array  so I am using following procedure
> >  
> > count<-1
> >  repeat{
> >  count<-count + 1
> >  c(g[count],1:i[count]) ->qw
> >  if(count>5)break
> >  }
>
> We can't reproduce this, as we don't have g or i.  But the general
> advice in a case like this is to simulate the loop by hand:  write down
> what is in each of the variables, and walk through the loop.
>
> I suspect your problem is in initializing count improperly, or in
> putting the test in the wrong place.

I think not. The assignment to "qw" only depends on "count", so it's small
wonder that the end result is that of the last iteration. If the
intention was to append to "qw", then you need something like

  qw <- c(qw, g[count],1:i[count])
 

> Duncan Murdoch
> >
> > as  a result qw is
> > [1]  0.9643836  1.0000000  2.0000000  3.0000000  4.0000000  5.0000000
> >  [7]  6.0000000  7.0000000  8.0000000  9.0000000 10.0000000 11.0000000
> > [13] 12.0000000 13.0000000 14.0000000 15.0000000 16.0000000 17.0000000
> > [19] 18.0000000 19.0000000 20.0000000 21.0000000 22.0000000 23.0000000
> > [25] 24.0000000 25.0000000 26.0000000 27.0000000 28.0000000 29.0000000
> > [31] 30.0000000 31.0000000 32.0000000 33.0000000 34.0000000 35.0000000
> > [37] 36.0000000 37.0000000 38.0000000 39.0000000
> >
> > which is according to the last value of  count i.e 6. What is the problem in error
> >  
> >
> > but I expect the result as
> >
> >  [1]  0.8328767  0.2410959  0.5315068  0.1424658  0.2520548  0.9643836
> >  [7]  6.0000000  7.0000000  8.0000000  0.2410959  1.0000000  2.0000000
> > [13]  3.0000000  4.0000000  5.0000000  0.5315068  1.0000000  2.0000000
> > [19]  3.0000000  4.0000000  5.0000000  6.0000000  7.0000000  8.0000000
> > [25]  9.0000000 10.0000000 11.0000000 12.0000000 13.0000000  0.1424658
> > [31]  1.0000000  0.0000000  0.2520548  1.0000000  2.0000000  3.0000000
> > [37]  4.0000000  5.0000000  6.0000000  7.0000000  8.0000000  9.0000000
> > [43] 10.0000000 11.0000000 12.0000000 13.0000000 14.0000000 15.0000000
> > [49] 16.0000000 17.0000000 18.0000000 19.0000000 20.0000000 21.0000000
> > [55] 22.0000000 23.0000000  0.9643836  1.0000000  2.0000000  3.0000000
> > [61]  4.0000000  5.0000000  6.0000000  7.0000000  8.0000000  9.0000000
> > [67] 10.0000000 11.0000000 12.0000000 13.0000000 14.0000000 15.0000000
> > [73] 16.0000000 17.0000000 18.0000000 19.0000000 20.0000000 21.0000000
> > [79] 22.0000000 23.0000000 24.0000000 25.0000000 26.0000000 27.0000000
> > [85] 28.0000000 29.0000000 30.0000000 31.0000000 32.0000000 33.0000000
> > [91] 34.0000000 35.0000000 36.0000000 37.0000000 38.0000000 39.0000000
> >
> > which is combination of 6 vectors of lengths {9,6,14,3,24,40).
> >
> >
> > Please tell me the error or alternate procedure for looping.
> >
> > regards,
> >
> > GS
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

--
   O__  ---- Peter Dalgaard             Ă˜ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html