[R] crazy loop error.

Petr PIKAL petr.pikal at precheza.cz
Tue Jan 25 09:33:30 CET 2011


Hi


r-help-bounces at r-project.org napsal dne 24.01.2011 23:18:35:

> Thanks for the reply Erik, As you mentioned, grouping consecutive 
elements
> of 'a' was my idea.
> I am unaware of any R'ish way to do it. It would be nice if someone in 
the
> community knows this.
> 
> The error resulting in the NA was pretty easy to fix, and my loop works, 
but
> the results are still wrong (new script below).
> Ideally it should print single "hello" for the single letters and 
grouped '3
> hellos' for the fives, grouped '2 hellos' for the sixes etc.
> 
> Based on the run results, if the value of n is being tracked, it changes
> quite unpredictably.
> Can someone explain how the value of n changes from end of the loop to 
the
> top without anything being done to it?

Put it into a foo function and make use debug(fun())

Regards
Petr

BTW mapply solution is shorter and probably quicker and easier to 
maintain.

> I cannot figure out what I am doing wrong.
> 
> a<-c(2,3,5,5,5,6,6,7)
> 
> for(n in 1:length(a))
> {
> print(paste("n: ",n))
> z1<-a[n]
> print(paste("z1:",z1))
> #make a list container
> ldata<-list()
> t=1
> 
> while(z1==a[n])
> {
> 
> #add dataframes to list
> ldata[[t]]<-paste("hello")
> 
> n=n+1
> t=t+1
> 
> if(n>length(a))
> {
> break;
> }
> }
> print("------End of while loop-------")
> 
> for(y in 1:length(ldata))
> {
> print(ldata[[y]])
> }
> 
> print(paste("n: ",n))
> print("******End of for loop********")
> }
> 
> 
> Thanks,
> Roy
> 
>    [[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.



More information about the R-help mailing list