Array
Array
Copilot
1. Arrays:
An array is a data structure that can store data in more than two dimensions.
Unlike vectors (uni-dimensional arrays) and matrices (two-dimensional arrays), arrays can have
more than two dimensions.
To create an array in R, we use the array() function.
The syntax for creating an array is: array(vector, dim = c(nrow, ncol, nmat)).
vector: Data items of the same type.
nrow: Number of rows.
ncol: Number of columns.
nmat: Number of matrices (product of nrow and ncol).
Example:
# Create a 2x3x2 array
array1 <- array(c(1:12), dim = c(2, 3, 2))
print(array1)
Output:
, , 1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
, , 2
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
Output:
Desired Element: 11
Output:
2nd Column Elements of 1st matrix: 3 4
1st Row Elements of 2nd Matrix: 7 9 11
Arrays are versatile for handling multi-dimensional data in R. If you have more questions or need further
examples, feel free to ask! 😊