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

From: Uwe Ligges <ligges_at_statistik.uni-dortmund.de>
Date: Tue 08 Mar 2005 - 02:29:14 EST

roger bos wrote:

> 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?

Parser, try ?"if"

And in ?"if" read the Details section, which tells you:

"[...] In particular, you should not have a newline between } and else to avoid a syntax error [...]".

The point is that

    if(A)

        B
is already syntactically complete (else can be omitted), so what the parser does not know what follows and has to evaluate ...

Uwe Ligges

> Thanks,
>
> Roger
>
> ______________________________________________
> R-help@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



R-help@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 Received on Tue Mar 08 02:51:59 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:41 EST