An Array in R Programming is simply called as the multi-dimensional Data structure. In R Array, data is stored in matrices, rows, and columns, and we can access the matrix element using the matrix level, row index, and column index.

In this article, we show how to Create an array in R, How to Access the Array Elements, Performing Arithmetic Operations on Arrays in R Programming with an example

## R Array Syntax

The syntax of the Array in R Programming language is

Array_Name <- array(data, dim = (row_Size, column_Size, matrices, dimnames)

If you observe the above code snippet, data is a Vector and

- Matrices: It will decide the number of Matrices an array can accept. Array in R is always a Multi-Dimensional, it means more than 1 matrix with rows and columns.
- Row_Size: Please specify the number of Row elements an array can store. For example, Row_Size =5, then each matrix in an array will have 5 rows.
- Column_Size: Number of Column elements an array can store. For example, Column_Size = 6, then each matrix in an array will have 6 Columns.
- dimnames: It is used to change the default Row, Column, Matrices names to more meaningful names.

## Create an Array in R

In this example, we will create an Array. The following code snippet will show you the most traditional way to create an Array in the R Programming language.

# Create A <- array(1: 24, dim = c(3, 4, 2)) print(A) vect1 <- c(10, 20, 30, 40) vect2 <- c(50, 60, 70, 80, 90, 100) B <- array(c(vect1, vect2), dim = c(3, 3, 2)) print(B)

The below statement will create an Array of elements from 1 to 24 arranged in two matrices, and each matrix contains three rows and four columns.

A <- array(1: 24, dim = c(3, 4, 2))

Next, we created two vectors

vect1 <- c(10, 20, 30, 40) vect2 <- c(50, 60, 70, 80, 90, 100)

From the above screenshot, you can observe that we are using c or concatenation function to combine those vectors to create an array of the elements are arranged in two Matrices. Each matrix will contain three rows and three columns.

B <- array(c(vect1, vect2), dim = c(3, 3, 2))

### Defining Row names and Column names for Array in R

In this example, we will show how to replace the default names of the Rows, Columns, and Matrix or define new names to the Rows, Columns, and Matrices in Arrays. We can achieve the same using dimnames as: x <- matrix(1:12, dimnames = list(rowNames, columnNames, matrixNames) .

A <- array(1: 24, dim = c(3, 4, 2)) print(A) # Defining Row names and Column names of Matrix in R row.names <- c("Row1", "Row2", "Row3") column.names <-c("Col1", "Col2", "Col3", "Col4") matrix.names <-c("Matrixl1", "Matrix2") B <- array(1: 24, dim = c(3, 4, 2), dimnames = list(row.names, column.names, matrix.names)) print(B)

## Accessing R Array Elements

In R programming, we can use the index position to access the array elements. Using the index, we can access or alter/change each and every individual element present in an array. Index value starts at 1 and ends at n where n is the size of a matrix, row, or column.

The syntax behind this R Array accessing is:

Array_Name[row_position, Column_Position, Matrix_Level].

For example, we declared an array of two matrices of size 6 rows * 4 columns. To access or alter 1st value use Array_name[1, 1, 1], to access or alter 2nd-row 3rd column value at 1st Matrix level then use Array_name[2, 3, 1] and to access the 6th-row 4th column in 2nd matrix level then use Array_name[6, 4, 2]. Let’s see the example for better understanding:

# Accessing Array Elements A <- array(1: 24, dim = c(3, 4, 2)) print(A) # Access the element of 1st row and 2nd column in Matrix 1. print(A[1, 2, 1]) # Access the element of 3rd row and 4th column in in Matrix 2. print(A[3, 4, 2]) # Access only the 3rd row in First Matrix. print(A[3, , 1]) # Access only the 4th column in Second Matrix. print(A[, 4, 2]) # Access the Complete First Matrix. print(A[ , , 1]) # Access the Complete Second Matrix. print(A[ , , 1])

### Access All elements of Array in R

In this example, we show you the process of accessing each and every element present in an Array

# Accessing Array Elements StudentArray <- array(10: 34, dim = c(3, 4, 2)) print(StudentArray) print(paste("Element at StudentArray[1, 1, 1] = ", StudentArray[1, 1, 1])) print(paste("Element at StudentArray[1, 2, 1] = ", StudentArray[1, 2, 1])) print(paste("Element at StudentArray[1, 3, 1] = ", StudentArray[1, 3, 1])) print(paste("Element at StudentArray[1, 4, 1] = ", StudentArray[1, 4, 1])) print(paste("Element at StudentArray[2, 1, 1] = ", StudentArray[2, 1, 1])) print(paste("Element at StudentArray[2, 2, 1] = ", StudentArray[2, 2, 1])) print(paste("Element at StudentArray[2, 3, 1] = ", StudentArray[2, 3, 1])) print(paste("Element at StudentArray[2, 4, 1] = ", StudentArray[2, 4, 1])) print(paste("Element at StudentArray[1, 1, 2] = ", StudentArray[1, 1, 2])) print(paste("Element at StudentArray[1, 2, 2] = ", StudentArray[1, 2, 2])) print(paste("Element at StudentArray[1, 3, 2] = ", StudentArray[1, 3, 2])) print(paste("Element at StudentArray[1, 4, 2] = ", StudentArray[1, 4, 2])) print(paste("Element at StudentArray[2, 1, 2] = ", StudentArray[2, 1, 2])) print(paste("Element at StudentArray[2, 2, 2] = ", StudentArray[2, 2, 2])) print(paste("Element at StudentArray[2, 3, 2] = ", StudentArray[2, 3, 2])) print(paste("Element at StudentArray[2, 4, 2] = ", StudentArray[2, 4, 2])) # To Access Third Row and 1, 2, 3, 4 Columns inside Ist Matrix print(paste("Element at StudentArray[3, 1, 1] = ", StudentArray[3, 1, 1])) print(paste("Element at StudentArray[3, 2, 1] = ", StudentArray[3, 2, 1])) print(paste("Element at StudentArray[3, 3, 1] = ", StudentArray[3, 3, 1])) print(paste("Element at StudentArray[3, 4, 1] = ", StudentArray[3, 4, 1])) # To Access Third Row and 1, 2, 3, 4 Columns inside 2nd Matrix print(paste("Element at StudentArray[3, 1, 1] = ", StudentArray[3, 1, 2])) print(paste("Element at StudentArray[3, 2, 1] = ", StudentArray[3, 2, 2])) print(paste("Element at StudentArray[3, 3, 1] = ", StudentArray[3, 3, 2])) print(paste("Element at StudentArray[3, 4, 1] = ", StudentArray[3, 4, 2]))

### Accessing Subset of a Array Elements

In our previous example, we show you how to access the single element from the R Array. In this example, we will show how to access the subset of multiple items from the Array. To achieve the same, we use the R Vector.

TIP: Negative index position is used to omit those values from an Array.

# Accessing Elements A <- array(1: 24, dim = c(3, 4, 2)) print(A) # Access the elements of 1st, 3rd row and 2nd, 4th column in Matrix 1. print(A[c(1, 2), c(3, 4), 1]) # Access All the element of 2nd and 3rd row in Matrix 2. print(A[c(2, 3), , 2]) # Access All the element of 1st and 4th Column in Matrix 1. print(A[ , c(1, 4), 1]) # Access All the element except 2nd row and 3rd Columm in Matrix 2. print(A[-2, -3, 2])

## R Array Addition and Subtraction

In this example, we show how to use Arithmetic Operators on Matrices to perform arithmetic Operations on Array.

# Adding and Subtracting Elements vect1 <- c(10, 20, 40 ) vect2 <- c(55, 67, 89, 96, 100) A <- array(c(vect1, vect2), dim = c(3, 4, 2)) print(A) mat.A <- A[, , 1] mat.B <- A[, , 2] print(mat.A + mat.B) print(mat.B - mat.A)

To perform the arithmetic operations, we are converting the multidimensional matrix into a one-dimensional matrix.

mat.A <- A[, , 1] mat.B <- A[, , 2]