Vectors

All of the preceding examples were concerned with single numbers, but most statistical analyses involve groups of numbers. In S-PLUS, groups of numbers can be manipulated most easily as vectors.

Suppose ten replications of an experiment had the following results:

2, 4.6, 1, 3.7, 5.9, 4.0, 6.7, 2.8, 1.4, 3.1
You can enter this data into S-PLUS as a vector, and store that vector as a variable (for instance, a variable called ``observations'').

> observations <- c(2, 4.6, 1, 3.7, 5.9, 4.0, 6.7, 2.8, 1.4, 3.1)
The function c() takes a list and returns it a vector (think of c as ``column''). Check the value of the variable observations by entering it at the prompt.

> observations
[1] 2.0 4.6 1.0 3.7 5.9 4.0 6.7 2.8 1.4 3.1
Manipulations on vectors work the same way as manipulations on single numbers. Suppose these observations were in inches but need to be converted to centimeters:

> 2.54*observations
[1]  5.080 11.684  2.540  9.398 14.986 10.160 17.018  7.112  3.556  7.874
Note that the vector observations still contains the original data.

> mean(observations)
[1] 3.52
> var(observations)
[1] 3.450667
The above functions return the mean and variance, respectively. Note that these functions will not work correctly unless they are given vectors as arguments. This first item below is an attempt to give mean three arguments, but it only expects one. The second item is the correct way: give mean a single argument, which happens in this case to be a vector with three elements.

> mean(1,3,5)
[1] 1
> mean(c(1,3,5))
[1] 3

Question: How would you normalize the vector observations to have mean 0 and variance 1?


Pantelis Vlachos
1/15/1999