Arrays

In this lesson, we will learn what arrays are in R and how to create them.

Both data structures (vectors and lists) are one dimensional meaning they have only length and no other dimension.

On the plus side, arrays in R language can be n-dimensional. However, like vectors, arrays can store only values having the same data type.

Creating Arrays

In R, arrays can be created using the array() keyword. Inputs to array() include concatenated vectors and the dim argument for creating an array.

Syntax for Creating Arrays

array(<dataVectors>, dim = <vectorOfDimensions>)

Optionally, we can also provide names for each dimension!

Let’s begin with making a simple two-dimensional array with one vector.

Press + to interact
myVector <- c (1, 2, 3) # create a vector
myArray <- array (myVector, dim = c(3, 1))
print (myArray)

The dimensions of this array are 3x13x1 where the number of rows is 33 and the number of columns is 11. This is also called a single column matrix.

Notice the row and column labels that are displayed on the console by using print() with an array.

Default row and column labels in an array
Default row and column labels in an array

The illustration above shows the meaning of the row and column labels displayed when an array is printed.

Now let’s code an array that has only one row but multiple columns:

Press + to interact
myVector <- c (1, 2, 3) # create a vector
myArray <- array (myVector, dim = c(1,3))
print (myArray)

The dimensions of this array are 1x31x3 where the number of rows is 11 and the number of columns is 33. This is also called a single row matrix.

Now let’s create an array of dimensions 2x22x2:

Press + to interact
myVector <- c (1, 2, 3, 4) # create a vector
myArray <- array (myVector, dim = c(2, 2))
print(myArray)

Now let’s see how to create an array of two 3x33x3 matrices each with 33 rows and 33 columns.

Press + to interact
myVector1 <- c (1, 2, 3)
myVector2 <- c (4, 5, 6)
myVector3 <- c (7, 8, 9)
myArray <- array (c(myVector1, myVector2, myVector3), dim = c(3, 3, 2))
print (myArray)

Here, in line number 5

dim = c(3, 3, 2)
# First argument is the number of rows, the second argument is the number of columns and the third argument is the number of matrices.

There are two matrices (notice the last argument) of size 3x33 x 3.

Now, let’s try making an array using vectors of different sizes.

Press + to interact
myVector1 <- c (1, 2, 3)
myVector2 <- c (4, 5, 6, 7, 8, 9)
myArray <- array (c(myVector1, myVector2), dim = c(3, 3, 2))
print (myArray)

Accessing and Modifying Arrays

We can access a specific element of the array using the square brackets []. Below is an example code to demonstrate how to access arrays.

Press + to interact
myVector1 <- c (1, 2, 3)
myVector2 <- c (4, 5, 6, 7, 8, 9)
myArray <- array (c(myVector1, myVector2), dim = c(3, 3, 2))
# Accessing the element located at row 1, column 1 in the first matrix
print (myArray[1, 1, 1])
# Accessing the element located at row 2, column 3 in the second matrix
print (myArray[2, 3, 2])

We can access an element based on a single index as well. R language uses column major order which means if only a single index is used to access an array, then the first nn indexes will belong to the first nn rows of the first column.

Let’s have a look at an illustration:

Column major indexes
Column major indexes

Let’s have a look at the code: In the illustration above "e" has index 55, and our code prints "e" when index 55 is accessed.

Press + to interact
myVector <- c ("a", "b", "c", "d", "e", "f", "g", "h", "i")
myArray <- array (myVector, dim = c(3, 3, 1))
print(myArray[5]) # Uses column major for accessing this index

We can also modify an element in an array using the two methods:

Illustration representing the index that needs to be modified
Illustration representing the index that needs to be modified
myVector <- c ("a", "b", "c", "d", "e", "f", "g", "h", "i")
myArray <- array (myVector, dim = c(3, 3, 2))
print("Original Array:")
print(myArray)
myArray[2, 2, 2] = "E" # Modifying the element located at row 2, column 2 in the second matrix
print("Modified Array:")
print (myArray)
Modifying based on row and column indexes