R Arrays Tutorial with Examples

In R, an array is a data structure that can store data in multiple dimensions. Unlike vectors, which are one-dimensional, and matrices, which are two-dimensional, arrays can be multi-dimensional (i.e., they can have more than two dimensions). Arrays in R are used to represent and manipulate data in multi-dimensional space.

This tutorial will cover:

Let’s explore each topic with detailed explanations and examples.

1. What is an Array in R?

An array is a multi-dimensional data structure in R that can hold data of the same type. The elements in an array are arranged in rows, columns, and additional dimensions. Arrays in R are primarily used to work with multi-dimensional data.

2. Creating an Array

You can create an array in R using the array() function. When creating an array, you need to specify the data and the dimensions (number of rows, columns, and other dimensions).

Syntax:

array(data, dim = c(nrow, ncol, ...))
  • data: The data to fill the array with.
  • dim: A vector of dimensions specifying the number of rows, columns, and dimensions.

Example (Creating a 2D Array):

# Create a 2D array (similar to a matrix)
arr_2d <- array(1:6, dim = c(2, 3))  # 2 rows, 3 columns
print(arr_2d)

Output:

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

Example (Creating a 3D Array):

# Create a 3D array with 2 rows, 3 columns, and 2 layers
arr_3d <- array(1:12, dim = c(2, 3, 2))
print(arr_3d)

Output:

, , 1

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

, , 2

     [,1] [,2] [,3]
[1,]    7    9   11
[2,]    8   10   12
  • The array() function creates a multi-dimensional array. In this example, arr_3d is a 3D array with two layers.

3. Accessing Elements in an Array

You can access specific elements in an array using square brackets ([ ]). To specify the position of an element, provide its row, column, and dimension.

Example (Accessing Elements in a 2D Array):

# Access the element in the first row, second column
element <- arr_2d[1, 2]
print(element)  # Outputs: 3

Example (Accessing Elements in a 3D Array):

# Access the element in the second row, first column, and second layer
element <- arr_3d[2, 1, 2]
print(element)  # Outputs: 8
  • You can specify multiple indices to access elements in multi-dimensional arrays.

4. Modifying Array Elements

You can modify specific elements in an array by assigning a new value to the desired position.

Example:

# Modify the element in the second row, third column of the first layer
arr_3d[2, 3, 1] <- 100
print(arr_3d)

Output:

, , 1

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4  100

, , 2

     [,1] [,2] [,3]
[1,]    7    9   11
[2,]    8   10   12
  • The element at position [2, 3, 1] is updated to 100.

5. Array Dimensions

You can retrieve or modify the dimensions of an array using the dim() function. This function returns a vector with the size of each dimension.

Example:

# Get the dimensions of the array
dims <- dim(arr_3d)
print(dims)  # Outputs: 2 3 2 (2 rows, 3 columns, 2 layers)
  • The dim() function provides the dimensions of the array.

6. Operations on Arrays

You can perform arithmetic operations on arrays element-wise, similar to how you would with vectors or matrices.

Example (Element-wise Operations):

# Create two 2D arrays
arr1 <- array(1:6, dim = c(2, 3))
arr2 <- array(6:1, dim = c(2, 3))

# Perform element-wise addition
sum_array <- arr1 + arr2
print(sum_array)

Output:

     [,1] [,2] [,3]
[1,]    7    7    7
[2,]    7    7    7
  • The two arrays are added element by element.

Example (Multiplying an Array by a Scalar):

# Multiply the array by a scalar
scaled_array <- arr1 * 2
print(scaled_array)

Output:

     [,1] [,2] [,3]
[1,]    2    6   10
[2,]    4    8   12
  • Each element in the array is multiplied by 2.

7. Applying Functions to Arrays

R provides functions like apply() to apply operations across the margins of an array (rows, columns, or other dimensions).

Example (Sum of Each Row):

# Sum across rows (MARGIN = 1 means rows)
row_sums <- apply(arr_2d, MARGIN = 1, FUN = sum)
print(row_sums)

Output:

[1] 9 12

Example (Sum of Each Column):

# Sum across columns (MARGIN = 2 means columns)
col_sums <- apply(arr_2d, MARGIN = 2, FUN = sum)
print(col_sums)

Output:

[1]  3  7 11
  • The apply() function allows you to perform operations across rows, columns, or any other dimension of the array.

8. Combining Arrays

You can combine arrays using functions like cbind() (column binding) and rbind() (row binding) for two-dimensional arrays, or abind() for higher-dimensional arrays.

Example (Combining Arrays by Columns):

# Combine two 2D arrays by columns
combined_array <- cbind(arr1, arr2)
print(combined_array)

Output:

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    3    5    6    4    2
[2,]    2    4    6    5    3    1
  • The two arrays are combined column-wise.

9. Multi-Dimensional Arrays

Arrays in R can have more than three dimensions. You can create higher-dimensional arrays by specifying more dimensions in the dim argument.

Example (4D Array):

# Create a 4D array with dimensions 2x2x2x2
arr_4d <- array(1:16, dim = c(2, 2, 2, 2))
print(arr_4d)

Output:

, , 1, 1

     [,1] [,2]
[1,]    1    3
[2,]    2    4

, , 2, 1

     [,1] [,2]
[1,]    5    7
[2,]    6    8

, , 1, 2

     [,1] [,2]
[1,]    9   11
[2,]   10   12

, , 2, 2

     [,1] [,2]
[1,]   13   15
[2,]   14   16
  • This creates a 4-dimensional array with 16 elements, arranged in four layers.

Summary of R Array Functions:

Function Description
array() Creates an array.
dim() Returns the dimensions of an array.

| | [ ] | Accesses elements of an array. | | apply() | Applies a function to the margins of an array. | | cbind() | Combines arrays by columns (for 2D arrays). | | rbind() | Combines arrays by rows (for 2D arrays). |

Conclusion

Arrays in R are powerful data structures for handling multi-dimensional data. Understanding how to create, access, and manipulate arrays allows you to work efficiently with more complex datasets. Here’s what we covered in this tutorial:

  • Creating arrays using the array() function.
  • Accessing elements by specifying row, column, and dimension indices.
  • Modifying array elements and performing element-wise operations.
  • Applying functions across rows, columns, and dimensions using apply().
  • Creating and working with multi-dimensional arrays.

Mastering arrays is essential for performing advanced data manipulation and analysis tasks in R.

Related posts

R Factors Tutorial with Examples

R Matrices Tutorial with Examples

R Vectors Tutorial with Examples