1. Array array() Function
myarray <- array(vector, dimensions, dimnames) #vector: data to store #dimensions: numeric vector #dimnames: list #The data pattern of the array is unique #An array is a three-dimensional data structure
2. Creating an Array
vector1 <- c(1,2,3) vector2 <- c(4,5,6,7,8) vector3 <- c(vector1,vector2) vector3[1] 1 2 3 4 5 6 7 8 class(vector3)[1] "numeric" myarray <- array(vector3, dim = c(3,3,2)) #Create 2 matrices of 3*3 myarray, , 1
[,1] [,2] [,3][1,] 1 4 7[2,] 2 5 8[3,] 3 6 1
, , 2
[,1] [,2] [,3][1,] 2 5 8[2,] 3 6 1[3,] 4 7 2
#Naming rows, columns, and matrices row.names <- c("Row1","Row2","Row3") column.names <- c("Col1","Col2","Col3") matrix.names <- c("Matrix1","Matrix2") myarray <- array(c(vector1,vector2),dim=c(3,3,2),+ dimnames = list(row.names,column.names,matrix.names)) myarray, , Matrix1
Col1 Col2 Col3Row1 1 4 7Row2 2 5 8Row3 3 6 1
, , Matrix2
Col1 Col2 Col3Row1 2 5 8Row2 3 6 1Row3 4 7 2
3. Accessing Array Elements
myarray[1,,] Matrix1 Matrix2Col1 1 2Col2 4 5Col3 7 8 myarray[,1,] Matrix1 Matrix2Row1 1 2Row2 2 3Row3 3 4 myarray[,,1] Col1 Col2 Col3Row1 1 4 7Row2 2 5 8Row3 3 6 1 myarray[1,1,1][1] 1 myarray[1,2,2][1] 5