[R] How "else" works

John Fox jfox at mcmaster.ca
Wed Feb 12 00:08:02 CET 2003


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
-----------------------------------------------------




More information about the R-help mailing list