[R] the less-than-minus gotcha

Kevin E. Thorpe kevin.thorpe at utoronto.ca
Mon Feb 2 02:38:16 CET 2015


Using = has it's problems too.

For example,

print(fit <- lm(...))

Assigns the result of the lm call to fit and prints the results. This is 
quite a useful trick actually.

print(fit = lm(...))

Throws an error.

Moral of story, computers do what you tell them, not what you meant.

Kevin

On 02/01/2015 08:26 PM, Steve Taylor wrote:
> All the more reason to use = instead of <-
>
>
> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ben Bolker
> Sent: Monday, 2 February 2015 2:07p
> To: r-help at stat.math.ethz.ch
> Subject: Re: [R] the less-than-minus gotcha
>
> Mike Miller <mbmiller+l <at> gmail.com> writes:
>
>>
>> I've got to remember to use more spaces.  Here's the basic problem:
>>
>> These are the same:
>>
>> v< 1
>> v<1
>>
>> But these are extremely different:
>>
>> v< -1
>> v<-1
>>
>
> This is indeed documented, in passing, in one of the pages you listed:
>
> http://tim-smith.us/arrgh/syntax.html
>
> Whitespace is meaningless, unless it isn't. Some parsing ambiguities
> are resolved by considering whitespace around operators. See and
> despair: x<-y (assignment) is parsed differently than x < -y (comparison)!
>


-- 
Kevin E. Thorpe
Head of Biostatistics,  Applied Health Research Centre (AHRC)
Li Ka Shing Knowledge Institute of St. Michael's
Assistant Professor, Dalla Lana School of Public Health
University of Toronto
email: kevin.thorpe at utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016



More information about the R-help mailing list