I have what is likely to be a simple question about the else keyword. The usage in the help pages is as follows: if(cond) cons.expr else alt.expr I would expect to be able to use it in the following way as well: if(cond){ cons.expr } else alt.expr This results a syntax error. Am I doing something wrong, or doesn't R support the spanning of the combination of if and else statements over several lines? Brian Gregor, P.E. Transportation Planning Analysis Unit Oregon Department of Transportation Brian.J.GREGOR at odot.state.or.us (503) 986-4120
Brian.J.GREGOR at odot.state.or.us wrote:> I have what is likely to be a simple question about the else keyword. > > The usage in the help pages is as follows: > > if(cond) cons.expr else alt.expr > > I would expect to be able to use it in the following way as well: > > if(cond){ > cons.expr > } > else alt.expr > > This results a syntax error. > > Am I doing something wrong, or doesn't R support the spanning of the > combination of if and else statements over several lines? >I believe this is the ``Introduction to R'' though where I am not sure. You need to have the following instead: if(cond1) { do action 1 } else if(cond2) { do action 2 } else { do action 3 } Regards, Sundar
Dear Brian, At 02:40 PM 2/11/2003 -0800, Brian.J.GREGOR at odot.state.or.us wrote:>I have what is likely to be a simple question about the else keyword. > >The usage in the help pages is as follows: > > if(cond) cons.expr else alt.expr > >I would expect to be able to use it in the following way as well: > > if(cond){ > cons.expr > } > else alt.expr > >This results a syntax error. > >Am I doing something wrong, or doesn't R support the spanning of the >combination of if and else statements over several lines?This construction will work in a function, but not directly at the command prompt, since the lines of the command preceding "else" are syntactically complete and hence not subject to continuation. The following (and variations) will work at the prompt, however: if (cond) { cons.expr } else alt.expr John ----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox -----------------------------------------------------
Brian.J.GREGOR at odot.state.or.us writes:> if(cond){ > cons.expr > } > else alt.expr > > This results a syntax error. > > Am I doing something wrong, or doesn't R support the spanning of the > combination of if and else statements over several lines?The rule is that in it read-eval-print loop, R will evaluate as soon as an expression is syntactically complete. Consider these two examples: (1) if (x == y) print("equal") b <- log(x) and (2) if (x == y) print("equal") else print("not equal") The problem is that in (1), you want output after reading the first line, and in the other, you don't. However, after reading the first line you cannot know which type of example you have. So R assumes it is of type (1), and that a new expression starts on the next line. However, an expression cannot begin with "else", hence you get a syntax error in case (2). The way out is to ensure that the first line remains incomplete, so if (x == y) print("equal") else print("not equal") if (x == y) print("equal") else print("not equal") {if (x == y) print("equal") else print("not equal")} all work. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On Tuesday 11 February 2003 04:40 pm, Brian.J.GREGOR at odot.state.or.us wrote:> I have what is likely to be a simple question about the else keyword. > > The usage in the help pages is as follows: > > if(cond) cons.expr else alt.expr > > I would expect to be able to use it in the following way as well: > > if(cond){ > cons.expr > } > else alt.expr > > This results a syntax error.What makes you say it does not work ? For example,> foo = function(x) {+ if (x == 1) { + print("one") + } + else { + print(x) + } + }> > foo(1)[1] "one"> foo(2)[1] 2>