Re: [R] Trying to perform an inner loop a random number of times

From: Galen Moore <galen.a.moore_at_gmail.com>
Date: Thu, 28 Apr 2011 02:34:17 -0600

Thanks very much, Jim.

I was incorrectly thinking that re-setting the variable feeding iter[i] would re-set iter[i] as well, I understand clearly now that it would and should not.

Re-initialization of my vectors immediately before the start of the inner loop did the trick, and my program now works perfectly.

What I sent was a forum-friendly version of my real program, which generates testing data for a study on space utilization and inferred preference in a small (<100) parking lot.

Each iteration of the outer (sp) loop represents a parking space (a fixed quantity), and each iteration of the inner loop creates the times in/out for the current space (hence the desire to run it a random number of times).

In case of use to others:
http://stackoverflow.com/questions/1882734/what-is-your-favorite-r-debugging -trick
http://www.biostat.jhsph.edu/~rpeng/docs/R-debug-tools.pdf

Thanks again,

Galen

-----Original Message-----
From: jim holtman [mailto:jholtman_at_gmail.com] Sent: Wednesday, April 27, 2011 21:32
To: galen.a.moore_at_gmail.com
Cc: r-help_at_r-project.org
Subject: Re: [R] Trying to perform an inner loop a random number of times

What exactly are you trying to accomplish? A little debugging might show you what the problem is. I put the following statement in the loop right after the assignment to 'newrow':

print(newrow)

What I saw is that you never reset 'iter' and once it reached a maximum length, you were continuing to use it so all the dataframes has the same size after a point. You can see it in your data in that all the last ones have a length of 3.

So your loop is doing exactly what you 'wrote' it to do. If you are not getting the results you expect, then learn how to debug. I put a 'browser()' call in you loop and this let me see what was going on. You need to learn to instrument your scripts so that you can trace the progress and see if it is working correctly.

On Wed, Apr 27, 2011 at 10:33 PM, Galen Moore <galen.a.moore_at_gmail.com> wrote:
> Grateful for any hints as to why I'm not getting the inner loop to
> cycle the expected number of times.
>
>
>
> Code and one run's results below.
>
>
>
> Thanks,
>
>
>
> Galen
>
>
>
>
>
>> # source("looptest.r")
>
>> sp<-numeric()
>
>> iter<-numeric()
>
>> rn<-numeric()
>
>> ds<-data.frame(sp, iter, rn)
>
>>
>
>> for (sp in c(1:6)) {
>
> +            i<-1
>
> +            while (i <= 5) {
>
> +                     rn<-0
>
> +                     rn<-round(runif(1, 1, 5))
>
> +                     if (i > rn) { break }
>
> +
>
> +                     else {
>
> +                         iter[i]<-i
>
> +                         newrow<-data.frame(sp, iter, rn)
>
> +                         i<-( i + 1)
>
> +                         }
>
> +                  }
>
> +                  ds<-rbind(ds, newrow)
>
> +             }
>
>> print(ds)
>
>   sp iter rn
>
> 1   1    1  3  >> sp #1 should appear 3x, not 2x
>
> 2   1    2  3
>
> 3   2    1  5  >> sp #2 should appear 5x, not 3x
>
> 4   2    2  5
>
> 5   2    3  5
>
> 6   3    1  5  >> sp #3 should appear 5x, not 3x
>
> 7   3    2  5
>
> 8   3    3  5
>
> 9   4    1  4  >> sp #4 should appear 4x, not 3x
>
> 10  4    2  4
>
> 11  4    3  4
>
> 12  5    1  3  >> this actually works as expected, but not 2x as with
> sp #1
>
> 13  5    2  3
>
> 14  5    3  3
>
> 15  6    1  5  >> sp #6 should appear 5x, not 3x
>
> 16  6    2  5
>
> 17  6    3  5
>
>>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Thu 28 Apr 2011 - 09:21:09 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 28 Apr 2011 - 09:40:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive