Storing data into variables is a fundamental operation in any
language, and R provides the arrow notation for the assignment
operator:
The assignment can operate in the opposite direction but it is not so
common, and is probably best avoided:
Occasionally you might also see the double headed arrow notation:
It is not generally used, should be avoided, and is included here only
for completeness. It is used, for example, to maintain information in
a function across different function calls to the same function, or to
share some information across a number of functions. In effect it
assigns a value to a variable that exists outside the function (in
fact, it searches through the list of environments up to the Global
Environment to find where the variable is defined, and assigns the
value to the first instance it finds, or creates a new one in the
Global Environment if none found). This breaks some of the principles
of functional programming--in that user variables might be changed
unexpectedly, possibly resulting in erroneous computations.
The single headed assignment will only assign to an object in the
current environment.
An alternative to <- is =, and in most situations
they are equivalent. There are subtle differences as in:
This gives a syntax error, whereas replacing = with
<- is quite legitimate (though unusual). This helps to avoid
a common coding bug where this test is not expected to be an
assignment, but in fact a comparison (which should really be using
==).
Also, the = assignment is actually used within function calls
to pass an object value through to the function body (a child
environment).
These three quite different assignment operations should be
distinguished syntactically, so although the S language allows the
<- to be replaced with = it is advised against. use
the syntax to highlight the different semantics!
Copyright © 2004-2008 Togaware Pty Ltd
Support further development through the purchase of the PDF version of the book.
PDF version is properly formatted and forms a comprehensive book (draft with over 600 pages).
Brought to you by Togaware.