An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110124/d8bc9bdc/attachment.pl>
crazy loop error.
16 messages · Roy Mathew, Erik Iverson, Petr Savicky +4 more
Roy, I have no idea what you're actually trying to do here, but it looks like there would be a more natural R'ish way if you're concerned about grouping consecutive elements of 'a'. At any rate, within your while loop, you're incrementing n by 1, and eventually n will be 10, which will be transformed to 11 when you add 1 to it, and a[11] will be NA, thus the error you receive...
Roy Mathew wrote:
Dear R-users,
This is a loop which is part of a bigger script. I managed to isolate the
error in this loop and simplified it to the bare minimum and made it
self-contained.
a<-c(2,3,4,5,5,5,6,6,6,7)
for(n in 1:10)
{
print(paste("n: ",n))
z1<-a[n]
#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
}
print("------End of while loop-------")
for(y in 1:length(ldata))
{
print(ldata[[y]])
}
print(paste("n: ",n))
print("******End of for loop********")
}
This script has a vector "a", for-loop, and a nested while-loop.
The for-loop runs from 1 to length of a. At every number of a, it enters the
while-loop and a hello is saved into list ldata.
If the next number in the vector a is a different number from previous then
the while-loop is exited and saved hello is printed.
If the next number in vector a is same as before then it loops inside the
while-loop and several hellos are printed together.
Then run-time error is
Error in while (z1 == a[n]) { : missing value where TRUE/FALSE needed
Thats because an NA creeps in somewhere. The problem can be seen far before
that. The full output from the run is below.
A lot of stuff was printed to help with the debugging. At n=4, there are
three repeats of 5, therefore hello is printed 3 times. n then becomes 7.
Then when the for-loop returns to top, n miraculously becomes 5. Hows
that!!?? Then on, everything goes wrong. I cannot figure out the problem.
[1] "n: 1"
[1] "------End of while loop-------"
[1] "hello"
[1] "n: 2"
[1] "******End of for loop********"
[1] "n: 2"
[1] "------End of while loop-------"
[1] "hello"
[1] "n: 3"
[1] "******End of for loop********"
[1] "n: 3"
[1] "------End of while loop-------"
[1] "hello"
[1] "n: 4"
[1] "******End of for loop********"
[1] "n: 4"
[1] "------End of while loop-------"
[1] "hello"
[1] "hello"
[1] "hello"
[1] "n: 7"
[1] "******End of for loop********"
[1] "n: 5"
[1] "------End of while loop-------"
[1] "hello"
[1] "hello"
[1] "n: 7"
[1] "******End of for loop********"
[1] "n: 6"
[1] "------End of while loop-------"
[1] "hello"
[1] "n: 7"
[1] "******End of for loop********"
[1] "n: 7"
[1] "------End of while loop-------"
[1] "hello"
[1] "hello"
[1] "hello"
[1] "n: 10"
[1] "******End of for loop********"
[1] "n: 8"
[1] "------End of while loop-------"
[1] "hello"
[1] "hello"
[1] "n: 10"
[1] "******End of for loop********"
[1] "n: 9"
[1] "------End of while loop-------"
[1] "hello"
[1] "n: 10"
[1] "******End of for loop********"
[1] "n: 10"
Error in while (z1 == a[n]) { : missing value where TRUE/FALSE needed
Mr Stuck-up.....
Thanks for any help.
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.
On Mon, Jan 24, 2011 at 07:16:58PM +0100, Roy Mathew wrote:
Dear R-users,
This is a loop which is part of a bigger script. I managed to isolate the
error in this loop and simplified it to the bare minimum and made it
self-contained.
a<-c(2,3,4,5,5,5,6,6,6,7)
for(n in 1:10)
{
print(paste("n: ",n))
z1<-a[n]
#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
}
print("------End of while loop-------")
for(y in 1:length(ldata))
{
print(ldata[[y]])
}
print(paste("n: ",n))
print("******End of for loop********")
}
This script has a vector "a", for-loop, and a nested while-loop.
The for-loop runs from 1 to length of a. At every number of a, it enters the
while-loop and a hello is saved into list ldata.
If the next number in the vector a is a different number from previous then
the while-loop is exited and saved hello is printed.
If the next number in vector a is same as before then it loops inside the
while-loop and several hellos are printed together.
Then run-time error is
Error in while (z1 == a[n]) { : missing value where TRUE/FALSE needed
Thats because an NA creeps in somewhere. The problem can be seen far before
that. The full output from the run is below.
A lot of stuff was printed to help with the debugging. At n=4, there are
three repeats of 5, therefore hello is printed 3 times. n then becomes 7.
Then when the for-loop returns to top, n miraculously becomes 5. Hows
that!!??
Hi.
The for-loop "for (i in 1:k)" uses an internal index, which counts
the repetitions. This is necessary, since the control over a loop
like "for (i in c(1,1,1,1))" cannot be based on the variable i only.
Hence, changing i does not influence the next iteration of the loop.
For example, the following loop always makes m*n repetitions, although
using the same variable in nested loops is definitely not suggested.
m <- 3
n <- 5
for (i in seq(length=m)) {
for (i in seq(length=n)) {
cat("*")
}
cat("\n")
}
Hope this helps.
Petr Savicky.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110124/e31847b1/attachment.pl>
Roy Mathew wrote:
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.
Is this the idea you're trying to execute? It uses ?rle and ?mapply. a <- c(2,3,5,5,5,6,6,7) mapply(rep, "hello", rle(a)$lengths, USE.NAMES = FALSE) [[1]] [1] "hello" [[2]] [1] "hello" [[3]] [1] "hello" "hello" "hello" [[4]] [1] "hello" "hello" [[5]] [1] "hello"
On Mon, Jan 24, 2011 at 11:18:35PM +0100, Roy Mathew wrote:
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?
Hi. A for-loop in R is different from a for-loop in C. It is similar to foreach loop in Perl. If v is a vector, then for (n in v) first creates the vector v and then always performs length(v) iterations. Before iteration i, n is assigned v[i] even if n is changed in the previous iteration. If you want to control the loop variable during execution, it is possible to use a while loop, where you have full control. While loop may be better also if v has a very large length, since, for example for (n in 1:1000000) creates a vector of length 1000000 in memory. It should also be noted that the for-loop for (n in 1:k) performs 2 iterations, if k is 0, since 1:0 is a vector of length 2. If k may be 0, then it is better to use for (n in seq(length=k)) since seq(length=0) has length 0. Hope this helps. Petr Savicky.
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.
On Tue, Jan 25, 2011 at 09:05:03AM +0100, Petr Savicky wrote:
[...]
to foreach loop in Perl. If v is a vector, then for (n in v) first creates the vector v and then always performs length(v) iterations.
I forgot that ?break? may stop the loop. See ?"for" for further information. In particular, it says You can assign to ?var? within the body of the loop, but this will not affect the next iteration. Petr Savicky.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110125/3690cc02/attachment.pl>
On Tue, 25 Jan 2011, Petr Savicky wrote:
On Mon, Jan 24, 2011 at 11:18:35PM +0100, Roy Mathew wrote:
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?
Hi. A for-loop in R is different from a for-loop in C. It is similar to foreach loop in Perl. If v is a vector, then for (n in v) first creates the vector v and then always performs length(v) iterations. Before iteration i, n is assigned v[i] even if n is changed in the previous iteration.
And also if v is changed during the loop.
If you want to control the loop variable during execution, it is possible to use a while loop, where you have full control. While loop may be better also if v has a very large length, since, for example for (n in 1:1000000) creates a vector of length 1000000 in memory. It should also be noted that the for-loop for (n in 1:k) performs 2 iterations, if k is 0, since 1:0 is a vector of length 2. If k may be 0, then it is better to use for (n in seq(length=k)) since seq(length=0) has length 0.
Since you keep mentioning that, it is actually much better to use seq_len(k) (and seq_along(x) instead of your earlier recommendation of seq(along=x)). And if you are using seq() in other cases in programs, consider seq.int() instead.
Hope this helps. Petr Savicky.
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110125/f059546d/attachment.pl>
Mr Ripley, May I ask why seq_len() and seq_along() are better than seq()? Thanks, Ivan Le 1/25/2011 09:58, Prof Brian Ripley a ?crit :
On Tue, 25 Jan 2011, Petr Savicky wrote:
On Mon, Jan 24, 2011 at 11:18:35PM +0100, Roy Mathew wrote:
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?
Hi. A for-loop in R is different from a for-loop in C. It is similar to foreach loop in Perl. If v is a vector, then for (n in v) first creates the vector v and then always performs length(v) iterations. Before iteration i, n is assigned v[i] even if n is changed in the previous iteration.
And also if v is changed during the loop.
If you want to control the loop variable during execution, it is possible to use a while loop, where you have full control. While loop may be better also if v has a very large length, since, for example for (n in 1:1000000) creates a vector of length 1000000 in memory. It should also be noted that the for-loop for (n in 1:k) performs 2 iterations, if k is 0, since 1:0 is a vector of length 2. If k may be 0, then it is better to use for (n in seq(length=k)) since seq(length=0) has length 0.
Since you keep mentioning that, it is actually much better to use seq_len(k) (and seq_along(x) instead of your earlier recommendation of seq(along=x)). And if you are using seq() in other cases in programs, consider seq.int() instead.
Hope this helps. Petr Savicky.
Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
Hi r-help-bounces at r-project.org napsal dne 25.01.2011 10:58:36:
ooh.. I have another question. What if I want to add the value in the vector a to the hello each time
it
prints. Here is your output a <- c(2,3,5,5,5,6,6,7) mapply(rep, "hello", rle(a)$lengths, USE.NAMES = FALSE) [[1]] [1] "hello" [[2]] [1] "hello" [[3]] [1] "hello" "hello" "hello" [[4]] [1] "hello" "hello" [[5]] [1] "hello" If I want something like this, based on values in vector a.
Not sure how to use mapply
test<-vector("list", 5)
lll<-rle(a)
for (i in seq_along(lll$lengths)) test[[i]] <- rep(paste("hello",
lll$values[i]), lll$lengths[i])
[[1]] [1] "hello 2" [[2]] [1] "hello 3" [[3]] [1] "hello 5" "hello 5" "hello 5" [[4]] [1] "hello 6" "hello 6" [[5]] [1] "hello 7" What i am actually doing is hmm.. I have a bunch of text files which is output from another program. I want to extract some specific information from these files and write to a new file and save it. All these files have a certain variable k which maybe 2, or 3 or 5 etc.
The
vector a shows the k values of 8 of such files. I want the contents of
all
files with k value 5 to be written into one file.
That is rather vague description. Does those files have some structure? How do you know the variable k? Loops are not so ineffective if you use them for what they are good and if you do not expand the object within loop. See R-Inferno from P.Burns. Regards Petr
Thanks, Roy On Mon, Jan 24, 2011 at 11:43 PM, Erik Iverson <eriki at ccbr.umn.edu>
wrote:
Roy Mathew wrote:
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.
Is this the idea you're trying to execute? It uses ?rle and ?mapply. a <- c(2,3,5,5,5,6,6,7) mapply(rep, "hello", rle(a)$lengths, USE.NAMES = FALSE) [[1]] [1] "hello" [[2]] [1] "hello" [[3]] [1] "hello" "hello" "hello" [[4]] [1] "hello" "hello" [[5]] [1] "hello"
-- Best Regards, 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.
Well, I'm not Prof. Ripley, but the answer is: Look at the code. seq_len, seq.int, and seq_along call Primitives, which are implemented in C, and therefore MUCH faster than seq(), which is implemented as pure R code (and is also a generic, so requires method dispatch). Though for small n (up to a few thousand, say), it probably doesn't make much difference.(Here, to be corrected by Prof. Ripley is needed). -- Bert On Tue, Jan 25, 2011 at 2:22 AM, Ivan Calandra
<ivan.calandra at uni-hamburg.de> wrote:
Mr Ripley, May I ask why seq_len() and seq_along() are better than seq()? Thanks, Ivan Le 1/25/2011 09:58, Prof Brian Ripley a ?crit :
On Tue, 25 Jan 2011, Petr Savicky wrote:
On Mon, Jan 24, 2011 at 11:18:35PM +0100, Roy Mathew wrote:
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?
Hi. A for-loop in R is different from a for-loop in C. It is similar to foreach loop in Perl. If v is a vector, then ?for (n in v) first creates the vector v and then always performs length(v) iterations. Before iteration i, n is assigned v[i] even if n is changed in the previous iteration.
And also if v is changed during the loop.
If you want to control the loop variable during execution, it is possible to use a while loop, where you have full control. While loop may be better also if v has a very large length, since, for example ?for (n in 1:1000000) creates a vector of length 1000000 in memory. It should also be noted that the for-loop ?for (n in 1:k) performs 2 iterations, if k is 0, since 1:0 is a vector of length 2. If k may be 0, then it is better to use ?for (n in seq(length=k)) since seq(length=0) has length 0.
Since you keep mentioning that, it is actually much better to use seq_len(k) (and seq_along(x) instead of your earlier recommendation of seq(along=x)). ?And if you are using seq() in other cases in programs, consider seq.int() instead.
Hope this helps. Petr Savicky.
-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
______________________________________________ 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.
Bert Gunter Genentech Nonclinical Biostatistics
Now I understand what the difference between a primitive and a non-primitive! Thanks for the clarification! Ivan Le 1/25/2011 18:03, Bert Gunter a ?crit :
Well, I'm not Prof. Ripley, but the answer is: Look at the code. seq_len, seq.int, and seq_along call Primitives, which are implemented in C, and therefore MUCH faster than seq(), which is implemented as pure R code (and is also a generic, so requires method dispatch). Though for small n (up to a few thousand, say), it probably doesn't make much difference.(Here, to be corrected by Prof. Ripley is needed). -- Bert On Tue, Jan 25, 2011 at 2:22 AM, Ivan Calandra <ivan.calandra at uni-hamburg.de> wrote:
Mr Ripley, May I ask why seq_len() and seq_along() are better than seq()? Thanks, Ivan Le 1/25/2011 09:58, Prof Brian Ripley a ?crit :
On Tue, 25 Jan 2011, Petr Savicky wrote:
On Mon, Jan 24, 2011 at 11:18:35PM +0100, Roy Mathew wrote:
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?
Hi. A for-loop in R is different from a for-loop in C. It is similar to foreach loop in Perl. If v is a vector, then for (n in v) first creates the vector v and then always performs length(v) iterations. Before iteration i, n is assigned v[i] even if n is changed in the previous iteration.
And also if v is changed during the loop.
If you want to control the loop variable during execution, it is possible to use a while loop, where you have full control. While loop may be better also if v has a very large length, since, for example for (n in 1:1000000) creates a vector of length 1000000 in memory. It should also be noted that the for-loop for (n in 1:k) performs 2 iterations, if k is 0, since 1:0 is a vector of length 2. If k may be 0, then it is better to use for (n in seq(length=k)) since seq(length=0) has length 0.
Since you keep mentioning that, it is actually much better to use seq_len(k) (and seq_along(x) instead of your earlier recommendation of seq(along=x)). And if you are using seq() in other cases in programs, consider seq.int() instead.
Hope this helps. Petr Savicky.
-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
______________________________________________ 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.
Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
On Tue, Jan 25, 2011 at 08:58:31AM +0000, Prof Brian Ripley wrote:
[...]
If k may be 0, then it is better to use for (n in seq(length=k)) since seq(length=0) has length 0.
Since you keep mentioning that, it is actually much better to use seq_len(k) (and seq_along(x) instead of your earlier recommendation of seq(along=x)). And if you are using seq() in other cases in programs, consider seq.int() instead.
Thank you for pointing out the functions seq_len(), seq_along() and seq.int(). These functions are primitive and faster, as others already mentioned. Using replicate(), i obtained on my computer a speed up by a factor between 5 and 7 for k <= 20 and there is a remarkable speed up also for larger k. The function seq.int() is more general than the other two. In particular, it can generate also a decreasing sequence. Petr Savicky.