[R] If statement generates two outputs

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Tue Mar 24 09:58:09 CET 2009


Berwin A Turlach wrote:
> G'day Carl,
>
> On Mon, 23 Mar 2009 20:11:19 -0400
> Carl Witthoft <carl at witthoft.com> wrote:
>
>
>   
>> But seriously:  can someone explain to me what's going on in the 
>> rvalues.r code?  I tried a simple experiment, replacing ":=" with a 
>> "colec" in the code, and of course the line
>>
>> c(df1, df2) colec list(4:8, 9:13)
>>
>>
>> just gives me a "syntax error" response.   Clearly I need a pointer
>> to some documentation about how the colon and equals sign get
>> "special treatment somewhere inside R.
>>     
>
> Not sure why := gets a special treatment, 

yet another bug??


> perhaps because it is not a
> valid name and, hence, the parser deduces that it is an operator?
>
>   

well, you can't do it with, e.g., ':==', because you'd get a *syntactic*
error (while ':=' gives a semantic error):

    a :== 1
    # syntactic error: unexpected '=' in ':='

    ':==' = function(a, b) NULL
    a :== 1
    # syntactic error again, of course

it's indeed surprising that it works with ':=' but not with, e.g., ':>'.

and in cases like ':-' you'd in fact use two operators (so an
'overloading' won't work for these):

    ':-' = function(a, b) a - if(a > b) b else 0
    2 :- 1
    # 2 1 0 -1
    # not 1

it's interesting to note that

    a :< b
    # error: unexpected '<' in ':<'

will tell you what's unexpected, while

    a :% b
    # error: unexpected input in 'a :% b'

    a :_ b
    # error: unexpected input in 'a :_'

will leave you wondering what's wrong there.



vQ




More information about the R-help mailing list