Understanding Arrays in R
Arrays are fundamental data structures in R, extending vectors to multiple dimensions. They are used to store elements of the same basic type (numeric, character, logical, etc.) in a structured, multi-dimensional grid. This makes them powerful for representing data with more than one index, such as matrices or cubes of information.
What is an Array?
Arrays are multi-dimensional generalizations of vectors.
An array in R is a data structure that can hold elements of the same data type, organized in one or more dimensions. Think of it as a grid or a cube of data.
Unlike vectors which are one-dimensional, arrays can have two, three, or more dimensions. Each element in an array is accessed using one or more indices, corresponding to its position along each dimension. This allows for efficient storage and manipulation of data that naturally fits a multi-dimensional structure.
Creating Arrays
Arrays are created using the
array()
The array()
function.
The
dim
dim = c(rows, columns)
dim = c(rows, columns, depth)
Consider a 2x3 array. This means it has 2 rows and 3 columns. If we have data 1:6
, array(1:6, dim = c(2, 3))
will arrange it into a 2x3 grid. The elements are filled column-wise by default. The first dimension represents rows, and the second dimension represents columns. For a 3D array, a third dimension (e.g., 'layers' or 'slices') is added.
Text-based content
Library pages focus on text content
Accessing Array Elements
Elements are accessed using square brackets
[]
my_array[2, 3]
For example, to get the element in the first row and second column of a 2x3 array
arr
arr[1, 2]
arr[1, ]
arr[, 2]
Array Dimensions and Attributes
You can inspect the dimensions of an array using the
dim()
length()
Function | Purpose |
---|---|
dim(array_name) | Returns the dimensions of the array (e.g., c(rows, columns, depth)). |
length(array_name) | Returns the total number of elements in the array. |
names(array_name) | Returns the names of dimensions (if assigned). |
Remember that R fills arrays column-wise by default. If you want to fill them row-wise, you can use the byrow = TRUE
argument in the array()
function.
Common Array Operations
Many operations that work on vectors and matrices can be extended to arrays. This includes arithmetic operations, applying functions to array elements or slices, and combining arrays.
Column-wise.
Learning Resources
A comprehensive guide to R arrays, covering creation, indexing, and manipulation with clear examples.
Explains the concept of R arrays, their syntax, and common operations with practical code snippets.
Provides a detailed overview of R arrays, including their properties, creation, and advanced usage patterns.
A foundational course that covers R basics, including vectors, matrices, and arrays, within a data science context.
A video lecture explaining R's core data structures, including arrays, as part of a broader R programming course.
The official R documentation for the `array()` function, detailing its arguments and behavior.
A clear and concise explanation of R arrays with practical examples for creating and accessing elements.
While focusing on tibbles, this chapter from Hadley Wickham's book provides context on R's fundamental data structures, including arrays.
An article that breaks down various R data structures, offering insights into how arrays fit into the broader ecosystem.
A collection of Q&A discussions on R arrays, useful for troubleshooting specific issues and understanding practical applications.