Skip to content

loop

4 messages · gynmeerut, Duncan Murdoch, Gichangi, Anthony +1 more

#
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
#
On 12/29/2005 7:41 AM, gynmeerut wrote:
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
#
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" <gynmeerut at indiatimes.com>
To: <r-help at stat.math.ethz.ch>
Sent: Thursday, December 29, 2005 1:41 PM
Subject: [R] loop
#
Duncan Murdoch <murdoch at stats.uwo.ca> writes:
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])