Changing the Length of an Object

An ``empty'' object may still have a mode. For example

e <- numeric()

makes e an empty vector structure of mode numeric. Similarly character() is a empty character vector, and so on. Once an object of any size has been created, new components may be added to it simply by giving it an index value outside its previous range. Thus

e[3] <- 17

now makes e a vector of length 3, (the first two components of which are at this point both NA). This applies to any structure at all, provided the mode of the additional component(s) agrees with the mode of the object in the first place.

This automatic adjustment of lengths of an object is used often, for example in the scan() function for input. (See §[*].)

Conversely to truncate the size of an object requires only an assignment to do so. Hence if alpha is an object of length 10, then

alpha <- alpha[2 * 1:5]

makes it an object of length 5 consisting of just the former components with even index. The old indices are not retained, of course.



Jeff Banfield
2/13/1998