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??
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
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.
Try: mxx = 2 mxy = 4 if (mxx > mxy) { print ("mxx reigns supreme") } else { print ("mxy reigns supreme") } -----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 9: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.
Beck, Kenneth (STP) <Kenneth.Beck at bsci.com> [Tue, Apr 29, 2008 at 07:21:40PM CEST]:> mxx=max(cpx_list$nMV); > mxy=max(trend_list$nMV); > if (mxx>mxy) > mxy=mxx > else > mxx=mxy >Can't this be replaced by mxx <- max(c(cpx_list$nMV, trend_list$nMV)) mxy <- mxx ? -- Johannes H?sing There is something fascinating about science. One gets such wholesale returns of conjecture mailto:johannes at huesing.name from such a trifling investment of fact. http://derwisch.wikidot.com (Mark Twain, "Life on the Mississippi")
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. >