LibraryArrays

Arrays

Learn about Arrays as part of R Programming for Statistical Analysis and Data Science

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

code
array()
function. This function takes a vector of data and a vector of dimensions as arguments.

What is the primary function used to create arrays in R?

The array() function.

The

code
dim
argument specifies the size of each dimension. For example,
code
dim = c(rows, columns)
for a 2D array (matrix), or
code
dim = c(rows, columns, depth)
for a 3D array.

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

code
[]
with indices for each dimension, separated by commas. If an index for a dimension is omitted, the entire slice along that dimension is returned.

How do you access the element in the 2nd row and 3rd column of an array named 'my_array'?

my_array[2, 3]

For example, to get the element in the first row and second column of a 2x3 array

code
arr
, you would use
code
arr[1, 2]
. To get the entire first row, you would use
code
arr[1, ]
. To get the entire second column, you would use
code
arr[, 2]
.

Array Dimensions and Attributes

You can inspect the dimensions of an array using the

code
dim()
function. The
code
length()
function returns the total number of elements.

FunctionPurpose
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.

What is the default filling order for elements when creating an array in R?

Column-wise.

Learning Resources

R Arrays - GeeksforGeeks(blog)

A comprehensive guide to R arrays, covering creation, indexing, and manipulation with clear examples.

R Data Structures: Arrays - Tutorialspoint(documentation)

Explains the concept of R arrays, their syntax, and common operations with practical code snippets.

R Programming/Arrays - Wikibooks(documentation)

Provides a detailed overview of R arrays, including their properties, creation, and advanced usage patterns.

Introduction to R for Data Science - DataCamp(tutorial)

A foundational course that covers R basics, including vectors, matrices, and arrays, within a data science context.

R Data Types and Structures - Coursera(video)

A video lecture explaining R's core data structures, including arrays, as part of a broader R programming course.

Arrays in R - RDocumentation(documentation)

The official R documentation for the `array()` function, detailing its arguments and behavior.

R Programming: Arrays - Programiz(blog)

A clear and concise explanation of R arrays with practical examples for creating and accessing elements.

R for Data Science: Data Structures - Hadley Wickham(blog)

While focusing on tibbles, this chapter from Hadley Wickham's book provides context on R's fundamental data structures, including arrays.

R Data Structures - Towards Data Science(blog)

An article that breaks down various R data structures, offering insights into how arrays fit into the broader ecosystem.

Arrays in R - Stack Overflow(documentation)

A collection of Q&A discussions on R arrays, useful for troubleshooting specific issues and understanding practical applications.