Vectors and Assignment

. operates on named data structures. The simplest such structure is the vector, which is a single entity consisting of an ordered collection of numbers. To set up a vector named x, say, consisting of five numbers, namely 10.4 , 5.6 , 3.1 , 6.4 and 21.7 , use the . command

x <- c(10.4, 5.6, 3.1, 6.4, 21.7)

This is an assignment statement using the function c() which in this context can take an arbitrary number of vector arguments and whose value is a vector got by concatenating its arguments end to end.[*]

A number occurring by itself in an expression is taken as a vector of length one.

Notice that the assignment operator is not the usual = operator, which is reserved for another purpose. It consists of the two characters < (`less than') and - (`minus') occurring strictly side-by-side and it `points' to the object receiving the value of the expression.[*]

Assignment can also be made using the function assign(). An equivalent way of making the same assignment as above is with:

assign("x", c(10.4, 5.6, 3.1, 6.4, 21.7))

The usual operator, <-, can be thought of as a syntactic short-cut to this.

Assignments can also be made in the other direction, using the obvious change in the assignment operator. So the same assignment could be made using

c(10.4, 5.6, 3.1, 6.4, 21.7) -> x

If an expression is used as a complete command, the value is printed and lost. So now if we were to use the command

1/x

the reciprocals of the five values would be printed at the terminal (and the value of x, of course, unchanged).

The further assignment

y <- c(x, 0, x)

would create a vector y with 11 entries consisting of two copies of x with a zero in the middle place.



Jeff Banfield
2/13/1998