I am trying to do the simplest thing in the world. The following works: aaa <- ifelse(aaa==5, 6, 7) But if I want to change the if...else syntax instead, it gives errors and assigns 7 to aaa. Here is the problem code: aaa <- 5 if ( aaa==5 ) { aaa <- 6 } else { aaa <- 7 } Here is the output:> aaa <- 5 > if ( aaa==5 ) {+ aaa <- 6 + }> else {Error: syntax error> aaa <- 7 > }Error: syntax error>Hope someone can solve this easy question for me. BTW, how come "?if" does not pull up the help file for the 'if' statement? Thanks, Roger
roger bos wrote:> I am trying to do the simplest thing in the world. The following works: > > aaa <- ifelse(aaa==5, 6, 7) > > But if I want to change the if...else syntax instead, it gives errors > and assigns 7 to aaa. Here is the problem code: > > aaa <- 5 > if ( aaa==5 ) { > aaa <- 6 > } > else { > aaa <- 7 > } > > Here is the output: > > >>aaa <- 5 >> if ( aaa==5 ) { > > + aaa <- 6 > + } > >> else { > > Error: syntax error > >>aaa <- 7 >>} > > Error: syntax error > >> > > > Hope someone can solve this easy question for me. > > BTW, how come "?if" does not pull up the help file for the 'if' statement?Parser, try ?"if" And in ?"if" read the Details section, which tells you: "[...] In particular, you should not have a newline between } and else to avoid a syntax error [...]". The point is that if(A) B is already syntactically complete (else can be omitted), so what the parser does not know what follows and has to evaluate ... Uwe Ligges> Thanks, > > Roger > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
from the help page of ?"if" (not "?if") you get: Note that it is a common mistake to forget to put braces `({..})' around your statements, e.g., after `if(..)' or `for(...)'. In particular, you should not have a newline between `}' and `else' to avoid a syntax error ... So you should use: aaa <- 5 if ( aaa==5 ) { aaa <- 6 } else { aaa <- 7 } Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "roger bos" <roger.bos at gmail.com> To: <R-help at stat.math.ethz.ch> Sent: Monday, March 07, 2005 4:16 PM Subject: [R] simple if...else causes syntax error>I am trying to do the simplest thing in the world. The following >works: > > aaa <- ifelse(aaa==5, 6, 7) > > But if I want to change the if...else syntax instead, it gives > errors > and assigns 7 to aaa. Here is the problem code: > > aaa <- 5 > if ( aaa==5 ) { > aaa <- 6 > } > else { > aaa <- 7 > } > > Here is the output: > >> aaa <- 5 >> if ( aaa==5 ) { > + aaa <- 6 > + } >> else { > Error: syntax error >> aaa <- 7 >> } > Error: syntax error >> > > Hope someone can solve this easy question for me. > > BTW, how come "?if" does not pull up the help file for the 'if' > statement? > > Thanks, > > Roger > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
On Mon, 7 Mar 2005, roger bos wrote:> I am trying to do the simplest thing in the world. The following works: > > aaa <- ifelse(aaa==5, 6, 7) > > But if I want to change the if...else syntax instead, it gives errors > and assigns 7 to aaa. Here is the problem code:Other people have told you how to fix this. I will point out in addition that if...else is not different syntax for ifelse() but a very different construct. ifelse() is a function that operates on vectors and returns a vector that is always the same length as the first argument. It does not change the flow of execution: all three of the arguments are evaluated. if(){} else {} chooses which branch of code to evaluate based on a single logical value. The value returned by this expression could be of completely different length and type depending on which code was evaluated. It might also be worth noting that the behaviour of newlines in terminating if() {} expressions is unavoidable in an interpreter using this syntax. When the user types if(condition){ some.code() } the interpreter cannot possibly tell whether an `else' clause is coming. Avoiding the problem would require a fairly significant change to the language, not just to the parser, eg adding an endif (the shell script solution), or requiring parentheses around the whole expression (the LISP solution, and in a sense the Python solution, though there the parentheses are invisible) -thomas
On Mon, Mar 07, 2005 at 10:16:50AM -0500, roger bos wrote:> I am trying to do the simplest thing in the world. The following works: > > aaa <- ifelse(aaa==5, 6, 7) > > But if I want to change the if...else syntax instead, it gives errors > and assigns 7 to aaa. Here is the problem code: > > aaa <- 5 > if ( aaa==5 ) { > aaa <- 6 > } > else { > aaa <- 7 > }This is due to R's (somewhat peculiar) semantics of newline, which R interprets as a terminator if an expression can terminate at the position of the newline, or else as a plain whitespace, see section on "Separators" in the R Language Definition. In the construction if ( aaa==5 ) { aaa <- 6 } R decides that the final newline can be a terminator, namely of an if- expression without an else branch. So, the if-expression is consumed by the parser and "forgotten" for the purpose of associating the else branch with it. The else branch thus appears to be astray and is reported as a syntax error. All this does not happen if the entire construct is enclosed in braces. Alternatively, the "else" can be placed on one line with the brace closing the if branch. Out of personal interest: Does anyone here know why the R parser was designed this way? Personally, I have been coding in R for years in the belief that newline is whitespace, and never even noticed any problems because all my ifs with elses were within functions and thus enclosed in curly braces. Best regards, Jan -- +- Jan T. Kim -------------------------------------------------------+ | *NEW* email: jtk at cmp.uea.ac.uk | | *NEW* WWW: http://www.cmp.uea.ac.uk/people/jtk | *-----=< hierarchical systems are for files, not for humans >=-----*