Scalars and Vectors

The simplest object in S-PLUS is a scalar. Scalars are single numbers (type ``numeric'') or words (type ``character''). Characters must be enclosed in quotes: "x" means the letter x, x means the object x. Similarly, 1 means the number one and "1" means the symbol used for the number one.

A vector is a set of scalars arranged in a one dimensional array (or alternatively, scalars are vectors of length 1). Vectors can only contain one type of scalar: either numbers or characters but not both. If S-PLUS sees both in the same vector it will change the numeric arguments to characters (by enclosing them in quotes).

> blah <- c(1,2,3,"d")
> blah
[1] "1" "2" "3" "d"
In the example above, S-PLUS converted the numbers 1,2 and 3 to their respective symbols so that all four elements in blah would be characters.

Use subscripts to see or change the value of a particular element or elements of a vector (this is covered in ``Introduction to S-PLUS'').

> blah[1]
[1] "1"
> blah[c(1,2,3)]
[1] "1" "2" "3"
> blah[4]
[1] "d"
> blah[4] <- "zip"
> blah
[1] "1"   "2"   "3"   "zip"
Vectors have a length attribute, which can be viewed or changed by using the function length.

> length(blah)
[1] 4
> length(blah) <- 5
> blah
[1] "1" "2" "3" "zip" ""
Note that when blah was changed to length five, the blank character ("") was added to the end of the vector as the fifth element. Had blah been a numeric vector, the new element would have been the blank numeric element (``NA'', for Not Available).

Question: What happens when you reduce the length of a vector?


Pantelis Vlachos
1/15/1999