[R] if else statement error

Marianne Promberger marianne.promberger at kcl.ac.uk
Sat Oct 3 21:33:35 CEST 2009


Two problems with your code:

(1) The "else" has to be on the same line as the closing "}"  -- this was
confusing for me as well.

(2) "and" probably should be "&", but read up on logical operators: ?&

Try:

ini=3
b=4
if (ini==1) {
  a=3
} else if (ini>1 & b>2 ) {
  a=3
} else {
  a=6
}


HTH,

Marianne



gcheer3 <gcheer3 at gmail.com> 03-Oct-09 17:08:
> 
> Hello,
>  
> I am doing a if else statement in R. But it always comes out error such as
> 'unexpected symbol' 
> There are two variables. ini and b. when ini=1, a=3; when ini>1 and b>2,
> a=3; 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
>      }
>     else if (ini>1 and b>2 ) {
>    a=3
>     }
>    else {a=6}
>  
>  
> Thanks a lot.
> -- 
> View this message in context: http://www.nabble.com/if-else-statement-error-tp25730173p25730173.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Marianne Promberger PhD, King's College London
http://promberger.info
R version 2.9.2 (2009-08-24)
Ubuntu 9.04




More information about the R-help mailing list