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 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.
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
==).
Copyright © 2004-2006 [email protected]
Support further development through the purchase of the PDF version of the book.
Brought to you by Togaware.