Dear R-users, I have a problem with the IF-ELSE syntax. Please look at the folllowing code and tell me what's wrong: a <- TRUE if ( a ) { cat("TRUE","\n") } else { cat("FALSE","\n") } If I try to execute with R I get: Error: syntax error, unexpected ELSE in "else" The strange thing is either "cat" instructions are executed!! My system is: Fedora Core 6 x86_64 + R 2.5.0 (rpm) Thank you very much in advance!!!! Regards, -- Marco
Shiazy Fuzzy wrote:> Dear R-users, > > I have a problem with the IF-ELSE syntax. > Please look at the folllowing code and tell me what's wrong: > > a <- TRUE > if ( a ) > { > cat("TRUE","\n") > }At this point, the expression above is complete and it ios evaluated if called interactively. Then, the code below is invalid (there is nothing that starts with "else").> else > { > cat("FALSE","\n") > }Instead, use: a <- TRUE if ( a ) { cat("TRUE","\n") } else { cat("FALSE","\n") } Uwe Ligges> If I try to execute with R I get: > Error: syntax error, unexpected ELSE in "else" > The strange thing is either "cat" instructions are executed!! > > My system is: Fedora Core 6 x86_64 + R 2.5.0 (rpm) > > Thank you very much in advance!!!! > > Regards, > > -- Marco > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code.
You need to put the else statement on the same line as the closing curly bracket. a <- TRUE if ( a ){ cat("TRUE","\n") } else { cat("FALSE","\n") } Cheers, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney> -----Oorspronkelijk bericht----- > Van: r-help-bounces op stat.math.ethz.ch > [mailto:r-help-bounces op stat.math.ethz.ch] Namens Shiazy Fuzzy > Verzonden: woensdag 20 juni 2007 11:41 > Aan: r-help op stat.math.ethz.ch > Onderwerp: [R] Got "Unexpected ELSE error" > > Dear R-users, > > I have a problem with the IF-ELSE syntax. > Please look at the folllowing code and tell me what's wrong: > > a <- TRUE > if ( a ) > { > cat("TRUE","\n") > } > else > { > cat("FALSE","\n") > } > > If I try to execute with R I get: > Error: syntax error, unexpected ELSE in "else" > The strange thing is either "cat" instructions are executed!! > > My system is: Fedora Core 6 x86_64 + R 2.5.0 (rpm) > > Thank you very much in advance!!!! > > Regards, > > -- Marco > > ______________________________________________ > R-help op 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 > and provide commented, minimal, self-contained, reproducible code. >
the problem is that you start `else' on a new line; check the following two solutions: if ( a ) { cat("TRUE", "\n") } else { cat("FALSE", "\n") } # or { if ( a ) { cat("TRUE", "\n") } else { cat("FALSE", "\n") } } I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Shiazy Fuzzy" <shiazy at gmail.com> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, June 20, 2007 11:41 AM Subject: [R] Got "Unexpected ELSE error"> Dear R-users, > > I have a problem with the IF-ELSE syntax. > Please look at the folllowing code and tell me what's wrong: > > a <- TRUE > if ( a ) > { > cat("TRUE","\n") > } > else > { > cat("FALSE","\n") > } > > If I try to execute with R I get: > Error: syntax error, unexpected ELSE in "else" > The strange thing is either "cat" instructions are executed!! > > My system is: Fedora Core 6 x86_64 + R 2.5.0 (rpm) > > Thank you very much in advance!!!! > > Regards, > > -- Marco > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Shiazy Fuzzy wrote:> Dear R-users, > > I have a problem with the IF-ELSE syntax. > Please look at the folllowing code and tell me what's wrong: > > a <- TRUE > if ( a ) > { > cat("TRUE","\n") > } > else > { > cat("FALSE","\n") > } > > If I try to execute with R I get: > Error: syntax error, unexpected ELSE in "else" > The strange thing is either "cat" instructions are executed!! >For some odd reason this is not actually a FAQ... It is an anomaly of the R (and S) language (or maybe a necessary consequence of its interactive usage) that it tries to complete parsing of expressions as soon as possible, so 2 + 2 + 5 prints "4" and then "5", whereas 2 + 2 + 5 prints "9". Similarly, when encountered on the command line, if (foo) bar will result in the value of bar if foo is TRUE and otherwise NULL. A subsequent else baz will be interpreted as a new expression, which is invalid because it starts with "else". To avoid this effect you can either move the "else" to the end of the previous line, or put braces around the whole if construct. I.e. if (foo) { bar } else { baz } or if (foo) bar else baz or { if (foo) bar else baz } should all work. -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907