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]]
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. > > -SeanThe 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)
Sean,> would like to receive expert opinion to avoid potential trouble[..]> 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.secure is relative, since due to R's lazy evaluation you never know whether a function's argument is being evalutated, look at:> f<- function(x)TRUE > x <- 1 > f((x=2)) # obscured attempt to assign in a function call[1] TRUE> x[1] 1 Thus there is dangerous advice in the referenced blog which reads: " f(x <- 3) which means "assign 3 to x, and call f with the first argument set to the value 3 " This might be the case in C but not in R. Actually in R "f(x <- 3)" means: call f with a first unevaluated argument "x <- 3", and if and only if f decides to evaluate its first argument, then the assignment is done. To make this very clear:> f <- function(x)if(runif(1)>0.5) TRUE else x > x <- 1 > print(f(x <- x + 1))[1] TRUE> print(f(x <- x + 1))[1] 2> print(f(x <- x + 1))[1] 3> print(f(x <- x + 1))[1] TRUE> print(f(x <- x + 1))[1] 4> print(f(x <- x + 1))[1] 5> print(f(x <- x + 1))[1] TRUE> print(f(x <- x + 1))[1] 6> print(f(x <- x + 1))[1] TRUE Here it is unpredictable whether your assignment takes place. Thus assigning like f({x=1}) or f((x=1))is the maximum dangerous thing to do: even if you have a code-reviewer and the guy is aware of the danger of f(x<-1) he will probably miss it because f((x=1)) does look too similar to a standard call f(x=1). According to help("<-"), R's assignment operator is rather "<-" than "=": " 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. " So my recommendation is 1) use R's assignment operator with two spaces around (or assign()) and don't obscure assignments by using C's assignment operator (or other languages equality operator) 2) do not assign in function arguments unless you have good reasons like in system.time(x <- something) HTH Jens Oehlschl?gel P.S. Disclaimer: you can consider me biased towards "<-", never trust experts, whether experienced or not. P.P.S. a puzzle, following an old tradition: What is going on here? (and what would you need to do to prove it?)> search()[1] ".GlobalEnv" "package:stats" "package:graphics" "package:grDevices" "package:utils" "package:datasets" "package:methods" [8] "Autoloads" "package:base"> ls(all.names = TRUE)[1] "y"> y[1] 1 2 3> identical(y, 1:3)[1] TRUE> y[] <- 1 # assigning 1 fails > y[1] 1 2 3> y[] <- 2 # assigning 2 works > y[1] 2 2 2> > # Tip: no standard packages modified, no extra packages loaded, neither classes nor methods defined, no print methods hiding anything, if you would investigate my R you would not find any false bottom anymore > > version_ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 8.1 year 2008 month 12 day 22 svn rev 47281 language R version.string R version 2.8.1 (2008-12-22) --
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.
I would argue that this is a matter of preference and the arguments on "principle" for one side or another are not particularly compelling. When the "=" was introduced for assignment, an argument was made that name=value function arguments are also implicitly a kind of assignment. While Duncan has pointed out a typical example of how this could be ambiguous, <- also has its problems. A syntactic confusion with "<-" arises in expressions like x<-3 which could be read either as an assignment or as a logical expression comparing x to -3. Perhaps obviously ambiguous when written naked like this but when buried in a larger expression, it's easy to write an expression like that and discover that you have overwritten x. Some might (and have, most emphatically) advise that we should routinely insert spaces in our typing in a way that disambiguates such expressions but I find an argument that relies on spaces, which are usually syntactically unmeaningful, not very compelling. Besides, I just find that clumsy two-character arrow ugly! This is an esthetic matter -- reminds me too much of an emoticon. :-( For over 10 years I used the alternative "_" underscore for S assignment (nice as a single character with no competing syntactic meaning) but that has gone away, as far as I can tell primarily due to discomfort (disdain?) of developers who find it unattractive because of the use of "_" as a connecting special category in OTHER contexts (C and shell programming). However, a mutually satisfactory solution is at hand!! If growth in the number of R programmers continues exponentially at its current rate, by the year 2097 the number of R programmers will exceed the population of the earth. At that point they will rise up and demand the restoration of the APL arrow key (remembered by some doddering guru who heard about it from her grandfather) to the standard keyboard, and our problem will be solved. (But there will be a minor irritation because in the New Zealand keyboard that key code will have been assigned to the locally popular sheep icon.) Alan Zaslavsky