[R] else if statement error

Duncan Murdoch murdoch at stats.uwo.ca
Sat Oct 3 18:58:07 CEST 2009


On 03/10/2009 11:54 AM, Chen Gu wrote:
> Hello,
> 
> I am doing a simple if else statement in R. But it always comes out error
> such as 'unexpected error'
> There are two variables. ini and b. when ini=1, a=3; when ini>1 and b>2,
> a=5; all other situations, a=6. I don't know where it is wrong.
> Here is my code
> 
> ini=3
> b=4
>      if (ini==1) {
>   a=3
>      }

The statement above is complete, so it will be evaluated.

>     else if (ini>1 and b>2 ) {

Then R sees the else, which can't start a new statement, and signals an 
error.  Put the else on the same line as the previous closing brace and 
things will be fine.

Duncan Murdoch

>    a=5
>     }
>    else {a=6}
> 
> 
> Thanks a lot.




More information about the R-help mailing list