Re: [R] If(cond) statement

From: Greg Snow <Greg.Snow_at_imail.org>
Date: Thu, 01 May 2008 10:23:10 -0600

This is for the same reason that:

3
+4

Does not give 7. R is optimized for interactive use, so if a statement can be considered complete, it evaluates it instead of waiting for more. In your case:

if(mxx>mxy)
  mxy=mxx

Is a complete statement and is evaluated without waiting to see if there is an else clause. Then it looks at the else and does not know what to do with it. To make sure that R waits until after you define the else clause before processing, write it like:

if(mxx>mxy){
  mxy <- mxx
 } else {
  mxx <- mxy
}

Here the {} groups make it clear that there is more to come.

Hope this helps,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow_at_imail.org
(801) 408-8111
 
 


> -----Original Message-----
> From: r-help-bounces_at_r-project.org
> [mailto:r-help-bounces_at_r-project.org] On Behalf Of Beck, Kenneth (STP)
> Sent: Tuesday, April 29, 2008 11:22 AM
> To: r-help_at_r-project.org
> Subject: [R] If(cond) statement
>
> 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 Thu 01 May 2008 - 17:17:21 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 Thu 01 May 2008 - 17:30:33 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