[R] Simple if else statement problem

Duncan Murdoch murdoch at stats.uwo.ca
Fri Nov 13 19:37:56 CET 2009


On 11/13/2009 12:54 PM, anna_l wrote:
> Ok Jim it worked, thank you! it´s funny because it worked with the first
> syntax in some cases...

If R knows the whole thing is incomplete, it will accept an else on its 
own line.  If the first 4 lines make a complete statement, R will 
evaluate it, and not accept the following else.

So putting braces around the whole thing is another workaround, but 
putting the else on the same line as the first closing brace is probably 
better.

Duncan Murdoch

> 
> 
> anna_l wrote:
>> 
>> Hello, I am getting an error with the following code:
>> if( P2 > P1)
>> + {
>> + P<-P2
>> + }
>>> else
>> Erro: unexpected 'else' in "else"
>>> {
>> + P<-P1
>> + }
>> 
>> I checked the syntax so I don´t understand, I have other if else
>> statements with the same syntax working. Thanks in advance
>> 
>




More information about the R-help mailing list