Similar to most other pro­gram­ming languages, R also lets de­velopers save elements of the same data type in an array. This data structure provides useful func­tion­al­it­ies, which de­velopers can benefit from when pro­gram­ming in R.

What can arrays in R be used for?

Like R strings, arrays are also a standard data structure in R. Arrays are par­tic­u­larly useful for or­gan­ising and accessing data. Through indexing, pro­gram­mers can ef­fi­ciently access data that they have stored in arrays.

R arrays make it easy to ef­fi­ciently perform op­er­a­tions that involve an entire dataset. By im­ple­ment­ing an array with multiple di­men­sions in R, de­velopers can also represent and work with mul­ti­di­men­sion­al data such as matrices and tensors.

How to create arrays in R

In R, you can create arrays with varying di­men­sions. They can be used to represent a simple vector or a complex mul­ti­di­men­sion­al structure. One of the most common types of arrays in is the two-di­men­sion­al array. You can think of this as a table or matrix with columns and rows.

You can create a simple two-di­men­sion­al 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 para­met­ers are passed to the array() function. The first parameter is the range of values:1:6. The second parament des­ig­nates the di­men­sions 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 di­men­sions for the array:

# Converting vectors into arrays
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 pro­gram­ming languages, you can do this by spe­cify­ing the indices of the elements in square brackets. If you are working with mul­ti­di­men­sion­al arrays, you can not only output in­di­vidu­al 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 ex­per­i­ence with other pro­gram­ming 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 cal­cu­la­tions with arrays

With R arrays, you can apply various math­em­at­ic­al 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 di­men­sions 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 arith­met­ic op­er­a­tions 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 cal­cu­la­tions. 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 para­met­ers:

  • array: Array to be con­sidered/used
  • MARGIN: Dimension that the function should be applied to, with 1 rep­res­ent­ing rows and 2 rep­res­ent­ing 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.

Go to Main Menu