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

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.ac.be
Mon Mar 7 16:29:27 CET 2005


from the help page of ?"if" (not "?if") you get:

Note that it is a common mistake to forget to put braces `({..})' 
around your statements, e.g., after `if(..)' or `for(...)'. In 
particular, you should not have a newline between `}' and `else' to 
avoid a syntax error ...

So you should use:

aaa <- 5
if ( aaa==5 ) {
   aaa <- 6
} else {
   aaa <- 7
}


Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "roger bos" <roger.bos at gmail.com>
To: <R-help at stat.math.ethz.ch>
Sent: Monday, March 07, 2005 4:16 PM
Subject: [R] simple if...else causes syntax error


>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
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list