[R] How "else" works

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Tue Feb 11 23:59:03 CET 2003


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




More information about the R-help mailing list