Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 72
4.3.5.1 Creating and Accessing Arrays
ОглавлениеArrays can be created with the array()
function; this function takes a “dim” attribute which defines the number of dimension. While arrays are similar to lists, they have to be of one class type (lists can consist of different class types).
array()
In the example we create an array with two elements, which are both three by three matrices.
# Create an array: a <- array(c(‘A’,‘B’),dim = c(3,3,2)) print(a) ## , , 1 ## ## [,1] [,2] [,3] ## [1,] "A" "B" "A" ## [2,] "B" "A" "B" ## [3,] "A" "B" "A" ## ## , , 2 ## ## [,1] [,2] [,3] ## [1,] "B" "A" "B" ## [2,] "A" "B" "A" ## [3,] "B" "A" "B" # Access one element: a[2,2,2] ## [1] "B" # Access one layer: a[,,2] ## [,1] [,2] [,3] ## [1,] "B" "A" "B" ## [2,] "A" "B" "A" ## [3,] "B" "A" "B"