[R] Definition of = vs. <-

Martin Morgan mtmorgan at fhcrc.org
Wed Apr 1 17:28:41 CEST 2009


Duncan Murdoch <murdoch at stats.uwo.ca> writes:

> On 4/1/2009 10:38 AM, Stavros Macrakis wrote:
>> NOTA BENE: This email is about `=`, the assignment operator (e.g. {a=1}
>> which is equivalent to { `=`(a,1) } ), not `=` the named-argument syntax
>> (e.g. f(a=1), which is equivalent to
>> eval(structure(quote(f(1)),names=c('','a'))).
>> As far as I can tell from the documentation, assignment with = is
>> precisely
>> equivalent to assignment with <-.  Yet they call different primitives:
>
> The parser does treat them differently:
>
>  > if (x <- 2) cat("assigned\n")
> assigned
>  > if (x = 2) cat("assigned\n")
> Error: unexpected '=' in "if (x ="
>
> The ?"=" man page explains this:
>
> " The  operator '<-' can be used anywhere,
>       whereas the operator '=' is only allowed at the top level (e.g.,
>       in the complete expression typed at the command prompt) or as one
>       of the subexpressions in a braced list of expressions. "
>
> though the restriction on '=' seems to be described incorrectly:
>
>  > if ((x = 2)) cat("assigned\n")
> assigned
>
> in which the assignment is in parentheses, not a braced list.
>
> As to the difference between the operations of the two primitives:
> see do_set in src/main/eval.c.  The facility is there to distinguish
> between them, but it is not used.

10.4.2 of R-lang shows they differ in precedence

> x = y <- 1
> rm(x,y)
> x <- y = 1
Error in (x <- y) = 1 : object 'x' not found

as reflected in main/names.c (PREC_LEFT vs. PREC_EQ)

{"<-",		do_set,		1,	100,	-1,	{PP_ASSIGN,  PREC_LEFT,	  1}},
{"=",		do_set,		3,	100,	-1,	{PP_ASSIGN,  PREC_EQ,	  1}},

and include/Defn.h

    PREC_LEFT    = 1,
    PREC_EQ	 = 2,

Martin

>
> Duncan Murdoch
>
>
>> 
>>> `=`
>> .Primitive("=")
>>> `<-`
>> .Primitive("<-")
>> (Perhaps these are different names for the same internal function?)
>> Also, the difference is preserved by the parser:
>> 
>>> quote({a=b})
>> {
>>     a = b
>> }
>>> quote({a<-b})
>> {
>>     a <- b
>> }
>> even though in other cases the parser canonicalizes variant syntax,
>> e.g. ->
>> to <-:
>> 
>>> quote({a->b})
>> {
>>     b <- a
>> }
>>> `->`
>> Error: object "->" not found
>> Is there in fact some semantic difference between = and <- ?
>> If not, why do they use a different operator internally, each
>> calling a
>> different primitive?
>> Or is this all just accidental inconsistency resulting from the '=',
>> <-',
>> and '->' features being added at different times by different people working
>> off different stylistic conventions?
>>             -s
>> 	[[alternative HTML version deleted]]
>> ______________________________________________
>> 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.
>
> ______________________________________________
> 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.

-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793



More information about the R-help mailing list