[R] simple if...else causes syntax error

roger bos roger.bos at gmail.com
Mon Mar 7 16:16:50 CET 2005


I am trying to do the simplest thing in the world.  The following works:

aaa <- ifelse(aaa==5, 6, 7)            
            
But if I want to change the if...else syntax instead, it gives errors
and assigns 7 to aaa.  Here is the problem code:

aaa <- 5
if ( aaa==5 ) { 
   aaa <- 6
}
else {
   aaa <- 7
}
            
Here is the output:

> aaa <- 5
>             if ( aaa==5 ) { 
+             aaa <- 6
+   }
>             else {
Error: syntax error
> aaa <- 7
> }
Error: syntax error
>             

Hope someone can solve this easy question for me.

BTW, how come "?if" does not pull up the help file for the 'if' statement?

Thanks,

Roger




More information about the R-help mailing list