Arrays in R

Arrays in R

  • R
  • 4 mins read

Arrays in R programming are a list of variables of the same type, grouped under a common name. It is a vector with several elements of the same type that can be accessed by its position (index). This article covers everything you need to know about Arrays in the R programming language. Let’s get started...

About Arrays in R Programming

Arrays are data structures that contain tabular data objects within them. Every array is associated with a fixed number of dimensions. The array data structures have at least 2 dimensions. There can be more than one array with the specified row and column dimensions.

For instance, if we create an array using dimensions = (r, c, n), their n matrices can be created with the order r x c. 

Syntax

The arrays can be created using the array() method in base R. The method has the following syntax: 

array ( data , dim = (rows, cols , num_of_mat, dim_names))
  • Where, data - The data values to arranged in the form of arrays 
  • rows - The number of rows in each matrix 
  • cols - The number of columns in each matrix 
  • num_of_mat - The number of matrices with dimensions rows x cols 
  • dim_names - The names to be assigned to the dimensions

Uni-dimensional arrays can be simply considered as vectors that can be declared with a number of rows and a number of matrices to be equivalent to 1 respectively. 

Creating arrays in R

Arrays can be created in R using the array() method that takes as input the data as well as the dimensions. Specifying the dimension names is optional in this method.

#creating data 
vec = 1:24
#creating array
#creating 2 matrices with dimensions 4x3
arr = array(vec,
            dim = c(4,3,2))
#printing the array
print("Arrays : ")
print(arr)
Output
[1] "Arrays : "
, , 1

     [,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12

, , 2

     [,1] [,2] [,3]
[1,]   13   17   21
[2,]   14   18   22
[3,]   15   19   23
[4,]   16   20   24

Naming the matrices in R

The matrices along with their row and column names can be named by specifying the named vectors and assigning them to the dimensions. 

#creating data 
vec = 1:24
#creating array
#creating 2 matrices with dimensions 4x3
arr = array(vec,
            dim = c(4,3,2))
#naming rows
row_names = c("r1","r2","r3","r4")
#naming columns
col_names = c("c1","c2","c3")
#naming matrices
mat_names = c("mat1","mat2")
#naming dimensions
dimnames(arr) = list(row_names,col_names,mat_names)
#printing the array
print("Arrays : ")
print(arr)
Output
[1] "Arrays : "
, , mat1

   c1 c2 c3
r1  1  5  9
r2  2  6 10
r3  3  7 11
r4  4  8 12

, , mat2

   c1 c2 c3
r1 13 17 21
r2 14 18 22
r3 15 19 23
r4 16 20 24

Accessing elements at the specified position 

The element of the n-dimensional array can be retrieved using the row index, column index, and matrix index respectively. The indexes specified should be well within the range of the dimensions of the array predefined. By default, the indexes of the array start with 1. The syntax used to retrieve the value:

  • array ( row-indx , col-indx , mat-indx)
  • where, row-indx : row index of the element to be retrieved 
  • col-indx : column index of the element to be retrieved 
  • mat-indx : matrix index of the element to be retrieved from 
#creating data 
vec = 1:24
#creating array
#creating 2 matrices with dimensions 4x3
arr = array(vec,
            dim = c(4,3,2))

#printing the array values
print(arr)

#accessing the element at specified position 
ele = arr[2,3,1]
#printing the element at specified position 
cat("Element value : ", ele)
Output
, , 1

     [,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12

, , 2

     [,1] [,2] [,3]
[1,]   13   17   21
[2,]   14   18   22
[3,]   15   19   23
[4,]   16   20   24

Element value :  10

It is also possible to access the entire row or column from the specified matrix, using the c() method. 

Conclusion

Arrays in R are one of the most popular data structures. Apart from being easy to understand and implement, they also have a lot of benefits associated with them as well. There are several different types of arrays in R programming. There can be uni-dimensional or multi-dimensional arrays.