Introduction to R 139
> x<-1:12
> x
[1] 1 2 3 4 5 6 7 8 9 10 11 12
then we can tell the system that it should treat this object as a matrix rather
than a one-dimensiona l array. We can do that by specifying the dimensions
of the matrix with the function dim:
> dim(x) <- c(3,4)
> x
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
From now on, the system will treat x as a two-dimensional array, a matrix
with 3 r ows and 4 columns. However, in r eality, nothing has changed in x.
Its values continue to be stored in the same memory locations as before and
indeed, they can be accessed using either a single index, specifying the position
of the desired element from the b eginning o f the arr ay:
> x[6]
[1] 6
or using two indexes, the way one normally