a shot of dev knowledge

Related tags

How to work with arrays in R

Arrays are one of the most useful data objects in R that allow the user to store data in multiple dimensions. For example, if we create an array with dimensions (4, 4, 2), it creates 2 rectangular matrices with 4 rows and 4 columns each.

Arrays are one of the most useful data objects in R, which allow the user to store data in multiple dimensions.

For example, If we create an array of dimension (4, 4, 2) then it creates 2 rectangular matrices each with 4 rows and 4 columns. Arrays can store only data type.

svg viewer

Creating an array

An array is created using the array() function.

The array() function takes the vector(s) and dimensions, dim, as parameters; it uses these vector(s) and values to create an array like shown below:

# Create two vectors of different lengths.
vector1 <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)

# Take these vectors as input to the array.
result <- array(c(vector1),dim = c(4,4,2))
print(result)

The values that we want in our rectangular matrices can be stored in a vector (or in multiple vectors) as shown in Line 2.

The output would be 2 rectangular matrices, each with 4 rows and columns (as specified in the dim)

We can also name the rows, columns of different matrices, and the matrices themselves. This is done by passing a list of the respective names to the dimnames parameter in the array() function, as shown below:

# Create two vectors of different lengths.
vector1 <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)

column_names <- c("COL1","COL2","COL3", "COL4")
row_names <- c("ROW1","ROW2","ROW3", "ROW4")
matrix_names <- c("Matrix1","Matrix2")

# Take these vectors as input to the array.
result <- array(c(vector1),dim = c(4,4,2),dimnames = list(row_names,column_names,
   matrix_names))
print(result)

Indexing and manipulating Array elements

Any value, in any of the matrices, can be easily indexed using the square brackets notation (where the row, column, and matrix number are specified)

svg viewer

The code below shows different examples of indexing values of an array in R.

  • Any cell value can also be modified by accessing/indexing it. In the code below, the first value of the second matrix is being set to 0 (See line 10). Now, when the second matrix prints, the updated value can be seen.

  • Various operations can also be performed on the parts that have been indexed from the array. For example, in the code below, the matrices have been extracted into separate variables. This enables relevant, binary operations to be performed on them (See line 15-18).

# 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])

result[1,1,2] = 0

# Print the 2nd Matrix.
print(result[,,2])

matrix1 = result[,,1]
matrix2 = result[,,2]

matrix3 = matrix1 + matrix2

print(matrix3)

Note: the numbering of rows and columns (and matrices too) starts from 1 (not 0)!

Related tags

RELATED COURSES
View all Courses
Related Courses
Related Courses
View all Courses

Keep Exploring