English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Arrays are also R language objects, and R language can create one-dimensional or multi-dimensional arrays.
R language array is a collection of the same type, and the matrix matrix we learned before is actually a two-dimensional array.
The relationship between vector, matrix, and array can be seen in the following figure:
R language array creation uses the array() function, which uses a vector as input parameter and can use dim to set array dimensions.
The syntax format of the array() function is as follows:
array(data = NA, dim = length(data), dimnames = NULL)
Parameter Description:
Data vector, array elements.
The dim of the array, which is a one-dimensional array by default.
The names of the dimnames dimension must be a list, and by default, no names are set.
In the following example, we create a 3 Rows 3 Two-dimensional array of columns:
# 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)
The output of the code above is:
, , 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 each dimension: :
# 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 each dimension result <- array(c(vector1,vector2), dim = c(3,3,2),dimnames = list(row.names,column.names,matrix.names)) print(result)
The output of the code above is:
, , 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
If you want to access array elements, you can use the column index and row index of the element, similar to a coordinate form.
# 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 the element at the third row of the second matrix in the array print(result[3,,,2]) # Display the element at the first row and third column of the first matrix in the array print(result[1,3,1]) # Output the second matrix print(result[,,2])
The output of the code above is:
COL1 COL2 COL3 3 12 15 [1] 13 COL1 COL2 COL3 ROW1 5 10 13 ROW2 9 11 14 ROW3 3 12 15
Since the array is composed of matrices of multiple dimensions, we can access array elements by accessing matrix elements.
# 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,9)) array2 <- array(c(vector1,vector2), dim = c(3,3,2)) # Create a matrix from an array matrix1 <- array1[,,2] matrix2 <- array2[,,2] # Matrix Addition result <- matrix1+matrix2 print(result)
The output of the code above is:
[1], [2], [3] [1,] 10 20 26 [2,] 18 22 28 [3,] 6 24 30
Additionally, we can use apply() Cross-dimensional calculation of array elements, syntax format as follows:
apply(x, margin, fun)
Parameter Description:
x array
margin Data Name
fun Calculation Function
Here we use the apply() function to calculate the sum of the numbers in each row of two matrices in the array.
# 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) # Sum of the first row of all matrices in the array result <- apply(new.array, c(1), sum) print(result)
The output of the code above is:
, , 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