[R] if syntax

ivo_welch-Rstat@mailblocks.com ivo_welch-Rstat at mailblocks.com
Sun Jun 20 18:00:39 CEST 2004


I ran into an interesting oddity of R,
    if (0) { print(1); }
    else  { print(2); }
 is a syntax error, while
    if (0) { print(1); } else  { print(2); }
or
    if (0) { print(1);
    } else  { print(2); }
  is not.  I presume it has to do with the duality of the newline 
functioning as an end of command (;) character, though it still seems a 
bit odd, and it took me a while to figure out what was wrong.  I 
eventually figured out that to resolve this ambiguity, I would guess 
that ifelse() would be a preferred function.

I wanted to look up the internal R documentation for if via "?if", but 
this does not work.  making the latter work would be a good idea.

regards,  /iaw
---
ivo welch
professor of finance and economics
brown / nber / yale




More information about the R-help mailing list