Re: [R] If(cond) statement

From: Erik Iverson <iverson_at_biostat.wisc.edu>
Date: Tue, 29 Apr 2008 13:22:34 -0500

Kenneth -

See ?if in the Details section. Specifically this part (at least in R 2.7),

"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'."

The R interpreter can't 'see ahead' that you have an else statement. Your program could validly end after your cons.expr, without the need for an 'else'. This is why you should put your else on the same line, possibly using {}s for clarity.

Best,
Erik Iverson

Beck, Kenneth (STP) wrote:
> Why will this simple statement not work? I think I am following the
> documentation for if(cond) statements, and I have tried wrapping the
> cons.expr and alt.expr in {}, I get the same error. There is no example
> in the help file, and this is not covered in the Introduction to R,
> SimpleR or other tutorials I have looked into.
>
> mxx=max(cpx_list$nMV);
> mxy=max(trend_list$nMV);
> if (mxx>mxy)
> mxy=mxx
> else
> mxx=mxy
>
> Error: unexpected 'else' in "else"
>
> Why does this error message have the first 'else' in single quote and
> second in double??
>
> ______________________________________________
> R-help_at_r-project.org 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.



R-help_at_r-project.org 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. Received on Tue 29 Apr 2008 - 20:16:24 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 29 Apr 2008 - 20:30:44 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive