[R] loop

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Dec 29 20:42:52 CET 2005


Duncan Murdoch <murdoch at stats.uwo.ca> 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
> > 
> > ______________________________________________
> > 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
> 
> ______________________________________________
> 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
> 

-- 
   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
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list