An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090311/1455e52e/attachment-0002.pl>
Is there any difference between <- and =
5 messages · Sean Zhang, Duncan Murdoch, David Smith +2 more
On 3/11/2009 10:18 AM, Sean Zhang wrote:
Dear R-helpers: I have a question related to <- and =. I saw very experienced R programmers use = rather than <- quite consistently. However, I heard from others that do not use = but always stick to <- when assigning valuese. I personally like = because I was using Matabl, But, would like to receive expert opinion to avoid potential trouble.
Use <- for assignment, and = for function arguments. Then the difference between f( a = 3 ) f( a <- 3 ) is clear, and you won't be surprised that a gets changed in the second case. If you use = for assignment, the two lines above will be written as f( a = 3 ) f( ( a = 3 ) ) and it is very easy to miss the crucial difference between them. Duncan Murdoch
Many thanks in advance. -Sean [[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.
On Wed, Mar 11, 2009 at 7:18 AM, Sean Zhang <seanecon at gmail.com> wrote:
Dear R-helpers: I have a question related to <- and =. I saw very experienced R programmers use = rather than <- quite consistently. However, I heard from others that do not use = but always stick to <- when assigning valuese. I personally like = because I was using Matabl, But, would like to receive expert opinion to avoid potential trouble. Many thanks in advance. -Sean
The short answer is that <- is used for assignment, and = is used to associate function arguments with their values. You can use = instead of <- for assignment too (in most contexts), but the converse isn't true. I've provided more detail about when you can and can't exchange the two operators (and some of the history about the operators themselves) in this blog post: http://blog.revolution-computing.com/2008/12/use-equals-or-arrow-for-assignment.html # David Smith
David M Smith <david at revolution-computing.com> Director of Community, REvolution Computing www.revolution-computing.com Tel: +1 (206) 577-4778 x3203 (Seattle, USA)
Duncan Murdoch wrote:
Use <- for assignment, and = for function arguments. Then the difference between f( a = 3 ) f( a <- 3 ) is clear, and you won't be surprised that a gets changed in the second case. If you use = for assignment, the two lines above will be written as f( a = 3 ) f( ( a = 3 ) ) and it is very easy to miss the crucial difference between them.
in fact, some recent posts show that things can go the other way round:
people try to use <- for function arguments. i think the following is
the most secure way if one really really has to do assignment in a
function call:
f({a=3})
and if one keeps this convention, <- can be dropped altogether.
vQ
1 day later
I think most people find it odd at first if they have always used "=" but quickly you get use to it and nothing could be more clear. It is explicit. It is active and provides a direction, a value goes into an object. The equal sign for assignment is ambiguous. As an example x = 3 we only know that the value 3 is assigned to the object x because by convention a number cannot be an object, if not it could be read as the object "3" taking the value "x" The expression literally states that they are equal, yet you cannot assume that all instances of 3 are equal to x, so it is an inaccurate expression. On the other hand, 3 -> x or x <- 3 is very clear. It makes no changes to "3" only to "x" I've been reading "Data Manipulation with R" and find the author's use of "=" for assignment disturbing. You quickly get use to -> and will find after a short time that you prefer it.
Sean Zhang wrote:
Dear R-helpers: I have a question related to <- and =. I saw very experienced R programmers use = rather than <- quite consistently. However, I heard from others that do not use = but always stick to <- when assigning valuese. I personally like = because I was using Matabl, But, would like to receive expert opinion to avoid potential trouble. Many thanks in advance. -Sean [[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.
View this message in context: http://www.nabble.com/Is-there-any-difference-between-%3C--and-%3D-tp22456167p22489108.html Sent from the R help mailing list archive at Nabble.com.