R Array: How to Create, Access, and Modify Array Elements

Array in R is a list or vector with two or more dimensions. An array is like a stacked matrix, and a matrix is a two-dimensional array.

R Array

R arrays are objects which can store data in more than two dimensions. Uni-dimensional arrays are called Vectors. A two-dimensional array is called Matrix. More than two-dimensional objects are called Arrays, which consist of all elements of the same data type.

How to Create an Array in R

To create an array in R, use the array() function. The array() function takes a vector as an argument and uses the dim parameter to create an array.

Syntax

array(data, dim = (nrow, ncol, nmat), dimnames=names)

Parameters

nrow: It is a number of rows.
ncol: It is a number of columns.
nmat: It is a number of matrices of dimensions nrow * ncol.
dimnames: The default value is NULL.

Example

Let’s define two vectors.

rv <- c(11, 19, 18)
rv2 <- c(21, 6, 29, 46, 37, 38)

You can take two vectors above as an input to an array where the dimension is considered 3 * 3, and two matrices or dimensional data is created.

rv <- c(11, 19, 18)
rv2 <- c(21, 6, 29, 46, 37, 38)

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

Output

, , 1

 [,1] [,2] [,3]
[1,] 11 21 46
[2,] 19 6 37
[3,] 18 29 38

, , 2

 [,1] [,2] [,3]
[1,] 11 21 46
[2,] 19 6 37
[3,] 18 29 38

In our example, the data is a vector, and the dimensions are (3, 3, 2), which means 3 rows, 3 columns, and 2 levels. The total number of elements in all the vectors combined should be equal to the number of elements in the matrices.

How to rename array in R

To rename an array, use the matrix.names property. The rows name changed to (“row1”, “row2”) and column names will be changed to (“column1”, “column2”) respectively.

array1 <- c(10, 20)
array2 <- c(30, 40)

r.names <- c("row1", "row2")
c.names <- c("col1", "col2")
m.names <- c("Arr1", "Arr2")

final <- array(c(array1, array2), dim = c(2, 2, 2), dimnames = list(r.names, c.names, m.names))
print(final)

Output

, , Arr1

    col1 col2
row1  10  30
row2  20  40

, , Arr2

     col1 col2
row1  10   30
row2  20   40

How to access Array elements in R

Array elements in R can be indexed using ‘[ ]’ wherein the array-like matrices consist of rows and columns, indexed by mat[row, column].

rv1 <- c(10, 20, 30, 40)
rv2 <- c(50, 60, 70, 80, 90)

r.names <- c("row1", "row2", "row2")
c.names <- c("col1", "col2", "col3")
m.names <- c("Arr1")

final <- array(c(rv1, rv2), dim = c(3, 3, 1), dimnames = list(r.names, c.names, m.names))
print(final)

Output

, , Arr1

    col1 col2 col3
row1 10   40   70
row2 20   50   80
row2 30   60   90

You can see the matrices from 1 to 9 are generated with (3, 3) dimensions (row, column) form, and the names of rows and columns are changed. Let’s see how the elements in the array can be extracted with the following examples.

Let’s extract the number ’50’ from the above array.

rv1 <- c(10, 20, 30, 40)
rv2 <- c(50, 60, 70, 80, 90)

r.names <- c("row1", "row2", "row2")
c.names <- c("col1", "col2", "col3")
m.names <- c("Arr1")

final <- array(c(rv1, rv2), dim = c(3, 3, 1), dimnames = list(r.names, c.names, m.names))
print(final[2, 2, 1])

The 50 valued element is positioned on (2, 2) on the 1st level array. See the below output.

[1] 50

Access multiple values of the array in R

To access multiple values at once, you need to specify the range you want.

rv1 <- c(10, 20, 30, 40)
rv2 <- c(50, 60, 70, 80, 90)

r.names <- c("row1", "row2", "row2")
c.names <- c("col1", "col2", "col3")
m.names <- c("Arr1")

final <- array(c(rv1, rv2), dim = c(3, 3, 1), dimnames = list(r.names, c.names, m.names))
print(final[1:2, 1:2, 1])

Output

     col1 col2
row1  10   40
row2  20   50

The above code gives the output as below where the value containing 2 rows, 2 columns, and 1 is the first array ‘Arr1‘ is extracted.

How to modify Array in R

As we know that an array is made up of matrices in multiple dimensions, the operations on items of the array are carried out by accessing items of the matrices.

To modify an array, reassign the item at the specific position, and it will change to a new array.

rv1 <- c(10, 20, 30, 40)
rv2 <- c(50, 60, 70, 80, 90)
arr1 <- array(c(rv1, rv2), dim = c(3, 3, 1))
print(arr1)
cat("After modifying the array", "\n")
arr1[2, 2, 1] <- 500
print(arr1)

Output

, , 1

 [,1] [,2] [,3]
[1,] 10 40 70
[2,] 20 50 80
[3,] 30 60 90

After modifying the array
, , 1

 [,1] [,2] [,3]
[1,] 10 40 70
[2,] 20 500 80
[3,] 30 60 90

Conclusion

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. That is it for R Array.

Leave a Comment