[R] If(cond) statement
Chuck Cleland
ccleland at optonline.net
Tue Apr 29 20:21:34 CEST 2008
On 4/29/2008 1:21 PM, 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??
Did you see this part of the help page?
"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
in entering a if ... else construct at the keyboard or via source. For
that reason, one (somewhat extreme) attitude of defensive programming is
to always use braces, e.g., for if clauses."
Try this:
if (mxx > mxy) mxy <- mxx else mxx <- mxy
Or this:
{if (mxx > mxy)
mxy <- mxx
else
mxx <- mxy}
> ______________________________________________
> 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.
--
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894
More information about the R-help
mailing list