🧮 R – Arrays: Create, Access, Modify, and Use Multi-Dimensional Data
Arrays in R are powerful data structures used to store multi-dimensional homogeneous data. Unlike vectors and matrices, which are limited to one and two dimensions respectively, arrays allow you to work with data in multiple dimensions, such as 3D, 4D, or more. This makes them ideal for statistical computing, data analysis, simulations, and scientific modeling.
In this article, we’ll explore how arrays work in R, how to create and access them, modify their elements, and perform common operations.
🔢 What is an Array in R?
An array is a data structure that holds elements of the same data type in more than two dimensions. In R:
- All values in an array must be numeric, character, or logical (homogeneous).
- Arrays are generalizations of vectors and matrices to multiple dimensions.
# Basic example
arr <- array(1:12, dim = c(3, 2, 2))
This creates a 3×2×2 array (3 rows, 2 columns, 2 “matrices”).
🛠️ Creating Arrays
You can create arrays using the array()
function:
# Syntax: array(data, dim = c(rows, columns, layers...))
arr <- array(1:24, dim = c(4, 3, 2))
✅ Explanation:
1:24
: Fills the array with numbers from 1 to 24dim = c(4, 3, 2)
: 4 rows, 3 columns, and 2 layers (3D array)
💡 Note:
If there are fewer elements than needed, R recycles them.
arr2 <- array(c(1, 2), dim = c(2, 2, 2))
🔍 Accessing Array Elements
You can access elements using [row, column, layer]
syntax:
arr[1, 2, 1] # Access element at row 1, column 2, layer 1
You can also access entire rows, columns, or slices:
arr[ , , 1] # All rows and columns of layer 1
arr[1, , ] # Row 1 across all layers
✏️ Modifying Array Elements
Modify elements the same way you access them:
arr[2, 3, 1] <- 99 # Set a specific element
arr[ , , 2] <- arr[ , , 2] * 2 # Modify a whole layer
➕ Array Operations
R supports arithmetic operations across arrays of the same shape:
arr1 <- array(1:8, dim = c(2, 2, 2))
arr2 <- array(9:16, dim = c(2, 2, 2))
result <- arr1 + arr2 # Element-wise addition
You can also perform:
*
,/
,-
operations- Logical comparisons (
>
,<
,==
)
🏷️ Naming Dimensions
You can name dimensions using dimnames
:
arr <- array(1:8, dim = c(2, 2, 2),
dimnames = list(
c("Row1", "Row2"),
c("Col1", "Col2"),
c("Matrix1", "Matrix2")
))
Access by name:
arr["Row1", "Col2", "Matrix1"]
🧮 Apply Function on Arrays
Use apply()
to perform operations along dimensions:
# Sum across rows (1st dimension)
apply(arr, 1, sum)
# Mean across columns (2nd dimension)
apply(arr, 2, mean)
📌 Syntax: apply(array, MARGIN, function)
- MARGIN = 1 (rows), 2 (columns), 3 (layers)
🆚 Array vs Matrix
Feature | Array | Matrix |
---|---|---|
Dimensions | 2+ (multi-dimensional) | 2 |
Homogeneous | ✅ Yes | ✅ Yes |
Use case | Multi-dimensional data | Linear algebra |
Creation | array() | matrix() |
Use arrays when working with more than two dimensions.
🧪 Exercises
1. Create a 3×3×2 array using values from 1 to 18.
# Your code:
2. Access the second row of the first matrix in a 3D array.
3. Multiply each element in the array by 10 using apply()
.
4. Add names to all dimensions and retrieve value using names.
✅ Summary
🔹 Arrays in R allow for flexible and structured multi-dimensional data storage.
🔹 Use the array()
function with a dim
argument to create arrays.
🔹 Access and modify elements using [row, col, layer]
syntax.
🔹 Perform arithmetic and logical operations directly on arrays.
🔹 Use apply()
to efficiently apply functions over specific dimensions.
🔹 Arrays are best used for 3D+ data, while matrices are ideal for 2D operations.
❓ FAQ – Frequently Asked Questions
🔸 Can an array contain different data types in R?
No. Arrays in R are homogeneous, meaning all elements must be of the same type.
🔸 How is an array different from a list in R?
Arrays are structured multi-dimensional containers for homogeneous data, while lists can store heterogeneous elements (different types).
🔸 How many dimensions can an R array have?
As many as your system memory can handle. Arrays are not limited to 3D.
🔸 Can I convert a matrix into an array?
Yes. A matrix is a 2D array. You can use dim()
to reshape it.
Share Now :