Page 234 - Data Science class 11
P. 234
6.10 arrays
Arrays are the R data objects in which we can store data in more than two dimensions. So, if you create an array of
dimensions (4,5,2), it will create two rectangular matrices, each with four rows and five columns. Arrays can only store
specific data types. If we create an array of dimension (2, 3, 4) then it creates 4 rectangular matrices, each with 2 rows
and 3 columns. Arrays can store only one data type.
An array is created using the array() function. It takes vectors as input and uses the values in the dim parameter to
create an array.
Example
The following example creates an array of two 3x3 matrices, each with 3 rows and 3 columns.
# Creating two vectors of different lengths.
vector1 <- c(5,9,3)
vector2 <- c(10,11,12,13,14,15)
# Taking these vectors as input to the array.
result <- array(c(vector1,vector2),dim = c(3,3,2))
print(result)
The print statement will cause the following to be displayed:
6.10.1 naming columns and rows
By using the dimnames parameter, we can give names to the rows, columns, and matrices in the array.
Example
Enter the following code snippet:
# Create two vectors of different lengths.
vector1 <- c(5,9,3)
vector2 <- c(10,11,12,13,14,15)
column.names <- c("COL1","COL2","COL3")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
# Take these vectors as input to the array.
232 Touchpad Data Science-XI

