Array
in R
Arrays are also objects in the R language, which can create one-dimensional or multi-dimensional arrays.
An array in R is a collection of the same type. The matrix we learned earlier is actually a two-dimensional array.
The relationships among vectors, matrices, and arrays can be seen in the diagram below:
Arrays in R are created using the array()
function, which takes vectors as input parameters and can use dim
to set the array dimensions.
The syntax format of the array()
function is as follows:
array(data = NA, dim = length(data), dimnames = NULL)
Parameter Description:
data
: Vector, the elements of the array.dim
: The dimensions of the array, defaulting to a one-dimensional array.dimnames
: The names of the dimensions, which must be a list. By default, no names are set.
The following example creates a 3x3 two-dimensional array:
Example
# Create two vectors of different lengths
vector1 <- c(5,9,3)
vector2 <- c(10,11,12,13,14,15)
# Create an array
result <- array(c(vector1,vector2),dim = c(3,3,2))
print(result)
Executing the above code outputs the following result:
, , 1
[,1] [,2] [,3]
[1,] 5 10 13
[2,] 9 11 14
[3,] 3 12 15
, , 2
[,1] [,2] [,3]
[1,] 5 10 13
[2,] 9 11 14
[3,] 3 12 15
Use the dimnames
parameter to set the names of the dimensions:
Example
# 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")
# Create an array and set the names of the dimensions
result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,column.names,matrix.names))
print(result)
Executing the above code outputs the following result:
, , Matrix1
COL1 COL2 COL3
ROW1 5 10 13
ROW2 9 11 14
ROW3 3 12 15
, , Matrix2
COL1 COL2 COL3
ROW1 5 10 13
ROW2 9 11 14
ROW3 3 12 15
Accessing Array Elements
To access array elements, you can use the column and row indices of the elements, similar to coordinate form.
Example
# 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")
# Create an array
result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names, column.names, matrix.names))
# Display elements of the third row in the second matrix
print(result[3,,2])
# Display the element in the first row and third column of the first matrix
print(result[1,3,1])
# Output the second matrix
print(result[,,2])
Executing the above code outputs the following result:
COL1 COL2 COL3
3 12 15
[1] 13
COL1 COL2 COL3
ROW1 5 10 13
ROW2 9 11 14
ROW3 3 12 15
Manipulating Array Elements
Since an array consists of multiple matrices, we can access the array elements by accessing the elements of the matrices.
Example
# Create two vectors of different lengths
vector1 <- c(5,9,3)
vector2 <- c(10,11,12,13,14,15)
# Create an array
array1 <- array(c(vector1,vector2),dim = c(3,3,2))
# Create two vectors of different lengths
vector3 <- c(9,1,0)
vector4 <- c(6,0,11,3,14,1,2,6)
# Create another array
array2 <- array(c(vector3,vector4),dim = c(3,3,2))
# Create matrices from these arrays
matrix1 <- array1[,,2]
matrix2 <- array2[,,2]
# Add the matrices
result <- matrix1 + matrix2
print(result)
Executing the above code outputs the following result:
[,1] [,2] [,3]
[1,] 11 20 26
[2,] 10 11 15
[3,] 6 12 16
vector4 <- c(6,0,11,3,14,1,2,6,9)
array2 <- array(c(vector1,vector2),dim = c(3,3,2))
# Create matrices from the array
matrix1 <- array1[,,2]
matrix2 <- array2[,,2]
# Add matrices
result <- matrix1+matrix2
print(result)
Executing the above code outputs:
[,1] [,2] [,3]
[1,] 10 20 26
[2,] 18 22 28
[3,] 6 24 30
Additionally, we can use the apply() function to perform calculations across array elements, with the syntax:
apply(x, margin, fun)
Parameters:
- x array
- margin data name
- fun calculation function
Below, we use the apply() function to calculate the sum of each row in the two matrices of the array.
Example
# Create two vectors of different lengths
vector1 <- c(5,9,3)
vector2 <- c(10,11,12,13,14,15)
# Create an array
new.array <- array(c(vector1,vector2),dim = c(3,3,2))
print(new.array)
# Calculate the sum of the numbers in the first row of all matrices in the array
result <- apply(new.array, c(1), sum)
print(result)
Executing the above code outputs:
, , 1
[,1] [,2] [,3]
[1,] 5 10 13
[2,] 9 11 14
[3,] 3 12 15
, , 2
[,1] [,2] [,3]
[1,] 5 10 13
[2,] 9 11 14
[3,] 3 12 15
[1] 56 68 60