Desktop Survival Guide
by Graham Williams


Storing data into variables is a fundamental operation in any language, and R provides the arrow notation for the assignment operator:

> x <- 10

The assignment can operate in the opposite direction but it is not so common, and is probably best avoided:

> 10 -> x

Occasionally you might also see the double headed arrow notation for assigning to the first occurrence of a variable in an enclosing environment, if the variable exists, or else in the global environment if the variable is not otherwise found:

> x <<- 10

It is not generally used and is best avoided. It 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:

  if (x=0) 1 else 2

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!

The general convention proposed by Ripley and Venables is thus:

x = y

Use this only in argument lists. Then x is a symbol in the child environment to which the value y from the current search path is assigned.

x <- y

Use this to assign the value y to the symbol x in the current environment.

> x <<- y

Use this to assign the value y either to the first occurrence of `x' in an enclosing environment, if such exists, or in the global environment if no such symbol currently exists.

Copyright © Togaware Pty Ltd
Support further development through the purchase of the PDF version of the book.
The PDF version is a formatted comprehensive draft book (with over 800 pages).
Brought to you by Togaware. This page generated: Sunday, 22 August 2010