R Arrays

R Arrays

Unlike matrices, an array can be of any number of dimensions. It means it can store data in more than two dimensions. It also takes vectors as input. To create an array it uses the dim parameter.

Example:

Let's create an array with two elements with 3X3 matrices each.

> vector1 <- c(100,200,300)
> vector2 <- c(400,500,600,700,800,900)
> result <- array(c(vector1,vector2),dim = c(3,3,2))
> result

Output:

, , 1
        [,1] [,2] [,3]
[1,]  100  400  700
[2,]  200  500  800
[3,]  300  600  900
, , 2
        [,1] [,2] [,3]
[1,]  100  400  700
[2,]  200  500  800
[3,]  300  600  900

Naming columns and Rows

We can give names to columns, rows, and matrices in the array with the dimnames parameter.

Example:

vector1 <- c(100,200,300)
vector2 <- c(400,500,600,700,800,900)
column.names <- c("COL1","COL2","COL3")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
# Take these vectors as input to the array.
result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,column.names,matrix.names))
print(result)

Output:

, , Matrix1
         COL1 COL2 COL3
ROW1  100  400  700
ROW2  200  500  800
ROW3  300  600  900
, , Matrix2
         COL1 COL2 COL3
ROW1  100  400  700
ROW2  200  500  800
ROW3  300  600  900

Accessing Array Elements

vector1 <- c(100,200,300)
vector2 <- c(400,500,600,700,800,900)
# Take these vectors as input to the array.
result <- array(c(vector1,vector2),dim = c(3,3,2))
print(result)
# Print the third row of the second matrix of the array.
print(result[3,,2])
# Print the element in the 1st row and 3rd column of the 1st matrix.
print(result[1,3,1])
# Print the 2nd Matrix.
print(result[,,2])

Output:

, , 1
[,1] [,2] [,3]
[1,]  100  400  700
[2,]  200  500  800
[3,]  300  600  900
, , 2
[,1] [,2] [,3]
[1,]  100  400  700
[2,]  200  500  800
[3,]  300  600  900
[1] 300 600 900
[1] 700
        [,1] [,2] [,3]
[1,]  100  400  700
[2,]  200  500  800
[3,]  300  600  900

Manipulating Array Elements

Example:

# Create two vectors of different lengths.
vector1 <- c(100,200,300)
vector2 <- c(400,500,600,700,800,900)
a1 <- array(c(vector1,vector2),dim = c(3,3,2))
vector1 <- c(10,20,30)
vector2 <- c(40,50,60,70,80,90, 10, 20, 30)
a2 <- array(c(vector1,vector2),dim = c(3,3,2))
# create matrices from these arrays.
m1 <- a1[,,2]
m2 <- a2[,,1]
print(m1)
print(m2)
# Add the matrices.
result <- m1+m2
print(result)

Output:

        [,1] [,2] [,3]
[1,]  100  400  700
[2,]  200  500  800
[3,]  300  600  900
       [,1] [,2] [,3]
[1,]   10   40   70
[2,]   20   50   80
[3,]   30   60   90
         [,1] [,2] [,3]
  [1,]  110  440  770
  [2,]  220  550  880
  [3,]  330  660  990

Calculations across array elements

To perform calculations across array elements we have to use apply() function.

Syntax:

apply(x, margin, fun)

Here, x is an array

margin is the data set name

fun is the function which you want to apply across the elements of the array

Example:

Let's see an example to find the sum of the elements in the rows of an array across all the matrices:

# Create two vectors of different lengths.
vector1 <- c(1,2,3)
vector2 <- c(4,5,6,7,8,9)
# Take these vectors as input to the array.
new.array <- array(c(vector1,vector2),dim = c(3,3,2))
print(new.array)
# Use apply to calculate the sum of the rows across all the matrices.
result <- apply(new.array, c(1), sum)
print(result)

Output:

, , 1
[,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
, , 2
[,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
[1] 24 30 36
Reference: https://www.tutorialspoint.com/r/r_arrays.htm https://www.rdocumentation.org/packages/base/versions/3.5.3/topics/array