Higher Dimensional Arrays
A higher
dimensional array is an array with more than two dimensions. In R programming
language, a higher dimensional array can be created using the array() function.
Syntax: array(data
= NA, dim = length(data), dimnames = NULL)
Parameters:
l data:
A vector or matrix containing the data for the array.
l dim:
A vector of integers specifying the dimensions of the array.
l dimnames:
An optional list of character vectors giving the names of the dimensions.
Ø
Types of Higher Dimensional
Arrays:
There
are several types of higher dimensional arrays in R, such as:
l Three-dimensional
arrays
l Four-dimensional
arrays
l Five-dimensional
arrays
l
n-dimensional arrays.
Creating
a Higher Dimensional Array:
To
create a three-dimensional array in R, we can use the array() function.
For
example, let's create a 3x3x2 array:
my_array
<- array(1:18, dim=c(3,3,2)) #
create a 3x3x2 array
print(my_array) #
print the array
Output:
, , 1
[,1] [,2]
[,3]
[1,] 1
4 7
[2,] 2
5 8
[3,] 3
6 9
, , 2
[,1] [,2]
[,3]
[1,] 10 13 16
[2,] 11 14 17
[3,] 12 15 18
Ø
Accessing and Modifying a Higher
Dimensional Array:
We can access
and modify the elements of a higher dimensional array using the same indexing
methods as for a matrix.
For
example, accessing
and modifying the elements
in the second row, third column, and first layer of the above array.
my_array[2,3,1] #
access the element in the second row, third column, and first layer
my_array[2,3,1]
<- 20 # modify the element
in the second row, third column, and first layer
print(my_array) #
print the modified array
Output:
[1] 8
, , 1
[,1] [,2]
[,3]
[1,] 1
4 7
[2,] 2 5
20
[3,] 3
6 9
, , 2
[,1] [,2]
[,3]
[1,] 10 13 16
[2,] 11 14 17
[3,] 12 15 18
Ø
Naming Columns and Rows in a
Higher Dimensional Array:
We can name the
rows, columns, and layers of a higher dimensional array using the dimnames()
function.
For
example, let's name the rows, columns, and layers of the
above array:
#
name the rows, columns, and layers of the array
dimnames(my_array)
<- list(c("row1", "row2", "row3"),
c("col1", "col2", "col3"))
Output :
col1 col2
col3
row1 ... ...
...
row2 ... ...
...
row3 ... ...
...
Ø
Manipulating Array Elements
As the array is
made up of matrices in multiple dimensions, the operations on elements of the
array are carried out by accessing elements of the matrices.
There
are various different operations
can be performed on Arrays.
Example:
# Create two vectors of different
lengths.
vector1 <- c(5, 9, 3)
vector2 <- c(10, 11, 12, 13, 14, 15)
# Take these vectors as input to the
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 matrices from these arrays.
matrix1 <- array1[,,2]
matrix2 <- array2[,,2]
# Add the matrices.
result <- matrix1 + matrix2
print(result)
Output:
[,1] [,2] [,3]
[1,] 10 20 26
[2,] 18 22 28
[3,] 6 24 30
No comments:
Post a Comment