[R] advice/opinion on "<-" vs "=" in teaching R

(Ted Harding) Ted.Harding at manchester.ac.uk
Fri Jan 15 11:01:58 CET 2010


On 15-Jan-10 09:29:16, Deepayan Sarkar wrote:
> On Fri, Jan 15, 2010 at 2:38 PM, Ted Harding
> <Ted.Harding at manchester.ac.uk> wrote:
>> On 15-Jan-10 08:14:04, Barry Rowlingson wrote:
>>> On Fri, Jan 15, 2010 at 6:57 AM, Ted Harding
>>> <Ted.Harding at manchester.ac.uk>wrote:
>>>>
>>>> There is at least one context where the distinction must be
>>>> preserved. Example:
>>>>
>>>> Â_pnorm(1.5)
>>>> Â_# [1] 0.9331928
>>>> Â_pnorm(x=1.5)
>>>> Â_# Error in pnorm(x = 1.5) : unused argument(s) (x = 1.5)
>>>> Â_pnorm(x<-1.5)
>>>> Â_# [1] 0.9331928
>>>> Â_x
>>>> Â_# [1] 1.5
>>>>
>>>> Ted.
>>>>
>>> I would regard modifying a variable within the parameters of a
>>> function call as pretty tasteless. What does:
>>>
>>> Â_foo(x<-2,x)
>>> or
>>> Â_foo(x,x<-3)
>>>
>>> do that couldn't be done clearer with two lines of code?
>>>
>>> Â_Remember: 'eschew obfuscation'.
>>>
>>> Barry
>>
>> Tasteless or not, the language allows it to be done; and therefore
>> discussion of distinctions between ways of doing it is relevant to
>> Erin's question!
>>
>> While I am at it, in addition to the above example, we can have
>>
>> Â_x <- 1.234
>> Â_sqrt(x=4)
>> Â_# [1] 2
>> Â_x
>> Â_# [1] 1.234
>>
>> compared with (as in the first example):
>>
>> Â_x <- 1.234
>> Â_sqrt(x<-4)
>> Â_# [1] 2
>> Â_x
>> Â_# [1] 4
>>
>> There is a passage in ?"<-" (which I don't completely understand)
>> which is also relevant to Erin's query about '=' vs '<-':
>>
>> Â_The operators '<-' and '=' assign into the environment in
>> Â_which they are evaluated. Â_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.
>>
>> (I'm not too clear about the scope of "one of the subexpressions
>> in a braced list of expressions").
> 
> For example:
> 
>> x = xyplot(1~1)
>> system.time(x = xyplot(1~1))
> Error in system.time(x = xyplot(1 ~ 1)) :
>   unused argument(s) (x = xyplot(1 ~ 1))
>> system.time({ x = xyplot(1~1) })
>    user  system elapsed
>   0.008   0.000   0.005
> 
> Of course, <- would not have had a problem. This is the most common
> problem I personally have had using = for assignment (better
> readability of <- is also a huge plus).
> 
> -Deepayan

Thanks for spelling out "braced list", Deepayan.
And I have to agree with you about the readability of "<-".

Indeed, as summary advice to Erin, I wouild say use "<-" except
in the specific context of assigning values to named parameters
in functions, to named elements of lists, and the like (where
use of "=" is mandatory anyway, or you won't get the desired
effect -- see below).

  list(x<-1.234,y<-2.345)
  # [[1]]
  # [1] 1.234
  # [[2]]
  # [1] 2.345

  list(x=1.234,y=2.345)
  # $x
  # [1] 1.234
  # $y
  # [1] 2.345

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 15-Jan-10                                       Time: 10:01:55
------------------------------ XFMail ------------------------------



More information about the R-help mailing list