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? 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@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.