An array is a data structure that can hold multi-dimensional data. In R, the array is objects that can hold two or more than two-dimensional data. For example, in square matrices can contain two rows and two columns and dimension can take five. Arrays can store the values having only a similar kind of data types. The data can be more than one dimensional, where there are rows and columns and dimensions of some length.
The array()
function will create an array which takes a vector, which is the numbers and dimension(dim
) in the argument.
Usage
array(data = NA, dim = length(data), dimnames = NULL)
Arguments
data: a vector (including a list or expression
vector) giving data to fill the array. Non-atomic classed objects are coerced by as.vector
.
dim: the dim attribute for the array to be created, that is an integer vector of length one or more giving the maximal indices in each dimension.
dimnames: either NULL
or the names for the dimensions. This must a list (or it will be ignored) with one component for each dimension, either NULL
or a character vector of the length given by dim
for that dimension. If the list is shorter than the number of dimensions, it is extended by NULL
s to the length required.
Creation of an Array
# Create two vectors
> v1 <- c(1, 2, 3)
> v2 <- c("a", "b", "c", "d", "e", "f")
# Take these vectors as input to your array.
> arr <- array(c(v1, 2),dim = c(3,3,2))
> print(arr)
, , 1
[,1] [,2] [,3]
[1,] "1" "a" "d"
[2,] "2" "b" "e"
[3,] "3" "c" "f"
, , 2
[,1] [,2] [,3]
[1,] "1" "a" "d"
[2,] "2" "b" "e"
[3,] "3" "c" "f"
Accessing elements of Array
The arrays can be accessed by using indices for different dimensions separated by commas. Different components can be specified by any combination of element’s names or positions.
# Print the third row of the second matrix of the array.
> print(arr[3,,2])
[1] "3" "c" "f"
# Print the first column of the first matrix of the array.
> print(arr[,1,1])
[1] "1" "2" "3"
# Print the element in the 1st row and 3rd column of the 1st matrix.
> print(arr[1,3,1])
[1] "d"
# Print the 2nd Matrix.
> print(arr[,,2])
[,1] [,2] [,3]
[1,] "1" "a" "d"
[2,] "2" "b" "e"
[3,] "3" "c" "f"
Manipulating Array Elements
As the array is made up of matrices in multiple dimensions, the operations on elements of an array are carried out by accessing elements of the matrices.
#Creating two vectors of different lengths
> v1 <-c(1:3)
> v2 <-c(10:15)
#Taking the vectors as input to the array1
res1 <- array(c(v1,v2),dim=c(3,3,2))
print(res1)
#Creating two vectors of different lengths
vec1 <-c(8,4,7)
vec2 <-c(16,73,48,46,36,73)
#Taking the vectors as input to the array2
> res2 <- array(c(vec1,vec2),dim=c(3,3,2))
> print(res2)
, , 1
[,1] [,2] [,3]
[1,] 1 10 13
[2,] 2 11 14
[3,] 3 12 15
, , 2
[,1] [,2] [,3]
[1,] 1 10 13
[2,] 2 11 14
[3,] 3 12 15
#Creating matrices from these arrays
> m1 <- res1[,,2]
> m2 <- res2[,,2]
> res3 <- m1 + m2
> print(res3)
[,1] [,2] [,3]
[1,] 2 20 26
[2,] 4 22 28
[3,] 6 24 30
Congratulations
Congratulations, you have made it to the end of this tutorial!
You’ve learned about R’s Array along with its creation, indexing in an array with examples, also with manipulation of matrices along with the array.
This brings the end of this Blog. We really appreciate your time.
Hope you liked it.
Do visit our page www.zigya.com/blog for more informative blogs on Data Science
Keep Reading! Cheers!
Zigya Academy
BEING RELEVANT