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

Thomas Lumley tlumley at u.washington.edu
Fri Jan 15 17:05:31 CET 2010


On Fri, 15 Jan 2010, 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?
>

It allows the lazy evaluation mechanism to determine whether the  assignment happens. That would be very hard to do in two lines of code.

         -thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle



More information about the R-help mailing list