Arrays
Arrays are numeric objects with dimension attributes. We start with the numbers 1 to 25 in a vector called array:
array<-1:25 is.matrix(array) [1] FALSE dim(array) NULL
The vector is not a matrix and it has no (NULL) dimensional attributes. We give the object dimensions like this (say, with five rows and five columns):
dim(array)<-c(5,5)
Now it does have dimensions and it is a matrix:
dim(array) [1] 5 5 is.matrix(array) [1] TRUE
When we look at array it is presented as a two-dimensional table (but note that it is not a table object; see p. 187):
array [,1] [,2] [,3] [,4] [,5] [1,] 1 6 11 16 21 [2,] 2 7 12 17 22 [3,] 3 8 13 18 23 [4,] 4 9 14 19 24 [5,] 5 10 15 20 25 is.table(array) [1] FALSE
Note that the values have been entered into array in columnwise sequence: this is the default in R. Thus a vector is a one-dimensional array that lacks any dim attributes. A matrix is a two-dimensional array. Arrays of three or more dimensions do not have any special names in R; they are simply referred to as three-dimensional or five-dimensional arrays. You should practise with subscript operations on arrays until you are thoroughly familiar with them. Mastering the use of subscripts will open up many of R's most powerful features for working with dataframes, vectors, matrices, arrays and lists. Here is a three-dimensional array of the first 24 lower-case letters with three matrices each of four rows and two columns:
A<-letters[1:24] dim(A)<-c(4,2,3) A , , 1 [,1] [,2] [ 1, ] ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access