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
This has been discussed several times on this list. Note that line 2 of paragraph 2 of help("if") says the following : " In particular, you should not have a newline between '}' and 'else' to avoid a syntax error in entering a 'if ... else' construct at the keyboard or via 'source'. " On Sun, 2004-06-20 at 17:00, ivo_welch-Rstat at mailblocks.com wrote:> 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 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
try ?"if" Best, Matthias ivo_welch-Rstat at mailblocks.com wrote:> > 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 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >-- Matthias Burger Bioinformatics R&D Epigenomics AG www.epigenomics.com Kleine Pr??sidentenstra??e 1 fax: +49-30-24345-555 10178 Berlin Germany phone: +49-30-24345-0
ivo_welch-Rstat at mailblocks.com wrote:> > I ran into an interesting oddity of R, > if (0) { print(1); } > else { print(2); }In both cases the ";" is superflously ..... If if(){}else{} is within an expression (e.g. a function's body), both ways work. Do you really want to use it outside a function's body? If so, I suggest to write: if(...){ ... } else{ ... }> 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.No. ifelse() is for the vectorized conditions. if(){} else{} is more efficient for length 1 conditions. Please read help("if") and help("ifelse").> 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.?"if" or help("if") do work perfectly. Uwe Ligges> regards, /iaw > --- > ivo welch > professor of finance and economics > brown / nber / yale > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
On Sun, Jun 20, 2004 at 09:00:39AM -0700, ivo_welch-Rstat at mailblocks.com wrote:> 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.FYI, it also works inside of (X)Emacs where the (highly recommend) ESS mode is kind enough to translate ?if into ?"if" so that the desired help is in fact shown. Hth, Dirk -- FEATURE: VW Beetle license plate in California