[R] Got "Unexpected ELSE error"

Shiazy Fuzzy shiazy at gmail.com
Wed Jun 20 12:02:15 CEST 2007


Thanks to everyone!!


I've just looked up on the R Language Definition manual:

"... When the if statement is not in a block the else, if present,
must appear on the same line as statement1. Otherwise the new line at
the end of statement1 yields a syntactically complete statement that
is evaluated. ..."

OK I admit I thought if-else statement was like the C version and I've
skipped that secition :P
(maybe this is a thing to insert in the "Introduction to R" manual)

Again, thank you!!

Sincerely,

-- Marco


On 6/20/07, Dimitris Rizopoulos <dimitris.rizopoulos at med.kuleuven.be> wrote:
> 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
>
>



More information about the R-help mailing list