[R] = or <-

Erik Iverson iverson at biostat.wisc.edu
Mon Jun 2 18:05:17 CEST 2008



Sébastien wrote:
> Thanks Erik and Gabor for this precision,
> 
> I guess that my misunderstanding of these concepts came from some bad 
> habits I took during my self-learning experience of R (especially, 
> copy/paste of old codes where <- was used for function arguments and did 
> not produce error messages).

Well it's not an error necessarily.  There are some situations where it 
might be useful to do an assignment to a variable in an argument list. 
However, this is the exception rather than the rule, especially for 
beginners.

You just have to be mindful of what is going on if you see '<-' in an 
argument list.

> 
> Sebastien
> 
> Erik Iverson a écrit :
>>
>>
>> Sébastien wrote:
>>> Dear R-users,
>>>
>>> I have written a short VB application to clean and format my R code. 
>>> Everything works fine except one small issue that I did not expected; 
>>> it is related the automatic replacement of assignment signs from "=" 
>>> to "<-". Most functions or arguments seem to accept either = or <-, 
>>> but some don't (e.g.  ls(all=TRUE)). The result is that my supposedly 
>>> clean codes do not run anymore :-(
>>> Is there a way to know the list of arguments/functions that do not 
>>> accept "<-" signs ?
>>
>> You are surely mixing two concepts here.
>>
>> When you are doing an assignment of a value to a variable, you may use 
>> either <- or =.  Example
>>
>> a <- 2
>> a = 2
>>
>> both bind the value of 2 to the symbol "a".
>>
>> When you call a function, for example ls, you can specify the argument 
>> names, such as ls(all = TRUE).
>>
>> If you attempted ls(all <- TRUE), the value of TRUE will be assigned 
>> to the symbol 'all', and then ls will be evaluated with its first 
>> argument "names" as TRUE, which produces your error.  You almost 
>> certainly want ls(all = TRUE).  After the error, try typing 'all' at 
>> the R prompt.  The 'all' function definition has now been overwritten 
>> and is simply TRUE, which you probably do not want either.
>>
>> So, there really is no concept of whether or not a function "accepts 
>> <- signs".  You want to use = in your function calls, since the '<-' 
>> method is actually assigning values.
>>
>> Hope that helps,
>> Erik
>>
>>
>>>
>>> Thanks in advance.
>>>
>>> Sebastien
>>>
>>> ______________________________________________
>>> 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.
>>
>>



More information about the R-help mailing list