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

An 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 arrays are objects which can store data in more than two dimensions.

How to Create an Array in R

To create an array in R, you can use the array() function. The 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 the number of rows.
ncol: It is several columns.
nmat: It is several 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 are 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. Therefore, the total number of elements in all the vectors combined should equal the number of elements in the matrices.

How to rename an array in R

To rename an array, use the matrix.names property. The rows name will be changed to (“row1”, “row2”), and the 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

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 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, an array comprises matrices in multiple dimensions, and the operations on the array are carried out by accessing items of the matrices.

To modify an array, reassign the item to the specific position, which 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

That’s it.

Leave a Comment