How to use arrays in R

Similar to most other programming languages, R also lets developers save elements of the same data type in an array. This data structure provides useful functionalities, which developers can benefit from when programming in R.

What can arrays in R be used for?

Like R strings, arrays are also a standard data structure in R. Arrays are particularly useful for organising and accessing data. Through indexing, programmers can efficiently access data that they have stored in arrays.

R arrays make it easy to efficiently perform operations that involve an entire dataset. By implementing an array with multiple dimensions in R, developers can also represent and work with multidimensional data such as matrices and tensors.

How to create arrays in R

In R, you can create arrays with varying dimensions. They can be used to represent a simple vector or a complex multidimensional structure. One of the most common types of arrays in is the two-dimensional array. You can think of this as a table or matrix with columns and rows.

You can create a simple two-dimensional array in R that contains the number 1-6 using the array() function:

examplearray <- array(1:6, dim = c(2, 3))
R

In the example above, two parameters are passed to the array() function. The first parameter is the range of values:1:6. The second parament designates the dimensions of the array. In this example, a 2x3 array has been created.

Note

Keep in mind that only elements with the same data type can be saved in arrays in R. If you want to store different types of data in the same data structure, you should use the data type list instead.

With array(), you can also convert vectors and matrices in your code into arrays. To do this, call the function that contains the element you want to cast into an array and then provide the dimensions for the array:

vector <- 1:9
vector_as_array <- array(vector, dim = c(3,3))
# Converting matrices into arrays
matrix <- matrix(1:9, nrow = 3, ncol = 3)
matrix_as_array <- array(matrix, dim = dim(matrix))
R

How to access array elements with indexing

Using indexing, you can access elements in your arrays. Similar to other programming languages, you can do this by specifying the indices of the elements in square brackets. If you are working with multidimensional arrays, you can not only output individual elements but also entire rows or columns:

examplearray <- array(1:6, dim = c(2, 3))
# Access the element in the first row and the second column
element <- examplearray[1, 2]
# Access the first row
row <- examplearray[, 1]
# Access the first column
column <- examplearray[1, ]
R
Note

If you are learning how to code and already have experience with other programming languages, indexing in R may seem somewhat strange. In contrast to other languages that begin with index 0, indexing in R starts with 1. This is similar to how we count everyday objects.

How to do calculations with arrays

With R arrays, you can apply various mathematical functions to entire datasets. For example, you can calculate the sum of two arrays. This would be like adding two matrices together. Arrays should have the same dimensions or length. You can find out the length of an R array with the length() function.

array1 <- array(1:4, dim = c(2,2))
array2 <- array(5:8, dim = c(2,2))
result <- array1 + array2
R

In addition to the arithmetic operations that you can implement using operators in R, various functions in R are defined to work with arrays. These can help you complete different types of calculations. For example, if you want to calculate the average of all elements in an array, you can use the R command mean():

average <- mean(array1)
R

You can also apply a range of functions to the dimension of your choice using the R array function apply(array, MARGIN, FUN). This function accepts several parameters:

  • array: Array to be considered/used
  • MARGIN: Dimension that the function should be applied to, with 1 representing rows and 2 representing columns
  • FUN: Vector function that returns a scalar result

Below is an example of how the function apply() can be used:

# Create an array
testarray <- array(1:6, dim = c(2,3))
# Use apply()
average_columns <- apply(array, MARGIN = 2, FUN = mean)
# Display the results
print(average_columns)
R

The code outputs three values, which are the averages of each column: 1.5 3.5 5.5.

Tip

Working on a web project in R? With webspace hosting from IONOS, you can choose between four different packages to find the right hosting option for your web project.

Was this article helpful?
Page top