Re: [R] priority of operators in the FOR ( ) statement

From: Patrick Burns <>
Date: Tue 23 Aug 2005 - 21:12:20 EST

The command that I think is most useful in this situation is 'browser()'.

Even a couple decades of programming in the S language hasn't yet solved the problem of my fingers typing code that doesn't match what I want to happen. I quite consistently have a


call in functions that I write to make sure that what I am assuming is the same as what R assumes.

Patrick Burns
+44 (0)20 8525 0696
(home of S Poetry and "A Guide for the Unwilling S User") wrote:

>Dear All,
>I spent an entire evening in debugging a small, fairly simple program in R
>- without success. It was my Guru in Bayesian Analysis, Thomas Fridtjof,
>who was able to diagonose the problem. He said that it took a long time
>for him also to locate the problem.
>This program illustrates in some ways the shortcomings of the error
>messages that R responds with. In this case, it was quite misleading and
>directs attention to a location far removed the actual problem statement.
>Without any more introductory comments, let me directly discuss the
>essential problem. I am enclosing the entire program after a brief
>The problem arises from the following statement (nr is an integer
>constant) :
>for ( i in 1:nr-1) {.......}
>The unexpected problem (at least for me) is that R reads the above
>statement as (i in (1:nr)-1) {.....}. This makes i be initially as zero
>which leads to an error because the for loop in R starts from 1. The
>problem is easily fixed by writing the for loop as ( i in 1:(nr-1))
>{.......}. This would be an easy problem to fix if R directly indicates
>what the problem is. Instead, it gives mystifying error messages which are
>totally misleading. For example, to the program given below, I got the
>following error message (these point to commands elsewhere in the program)
>Error in if ((x >= 0) & (x < s2)) return(x/2) else if ((x >= s2) & (x < :
> missing value where TRUE/FALSE needed
>I would like clarifications on the following points :
>1. I am just curious to know if the priority of operators in the for
>statement ( the colon before the minus operator, for example) is a
>deliberate design decision. I have tested Matlab and found that it
>interprets my original statement correctly without an extra paranthesis.
>2. Faced with a similiar problem in the future, what is a smart way of
>debugging in R to locate a problem. With this problem, I checked and
>double checked every single statement in the program, except the for
>statement because I just did not expect any problem there. I have seen
>that there is a debug package but I have not used it. Can such tools be
>used to locate a problem with greater ease? Can somebody give a concrete
>example (for the following program, for example) of a debugging routine.
># Bayesian Data Analysis
>## source("M:/programming/Rfolder/Assignments/fortest.txt")
># #Remove all objects from the workspace
># #We will also try to note the time that the program takes
># #We will start the clock at starttime
>starttime <- proc.time()[3];
>my.function<-function(x) {
>if ((x>=0) & (x<s2)) return(x/2)
>if ((x>=s2) & (x<1+s2)) return(0.2)
>if ((x>=1+s2) & (x<1.5+s2)) return(0.6)
>if ((x>1.5+s2) | (x<0)) return(0)
>alphayx<-function(y,x) {
># to account for 0/0 division
>if ( fyx<-0
>#nr is the number of iterations
>for (i in 1:nr-1) {
>cat("Elapsed time is", elapsedTime, "seconds", "\n")
>This message is meant for the addressee only and may contain
>confidential and legally privileged information. Any unauthorised
>review, use, copying, storage, disclosure or distribution of this e-
>mail and any attachments is strictly prohibited. If you are not the
>named recipient or have otherwise received this communication in
>error, please destroy this message from your system and kindly notify
>the sender by e-mail. Thank you for your co-operation.
> [[alternative HTML version deleted]]
> mailing list
>PLEASE do read the posting guide!
> mailing list PLEASE do read the posting guide! Received on Tue Aug 23 21:17:36 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:39:55 EST