R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Arrays in R can be thought of as multi-dimensional matrices or as a collection of matrices. They can be 1D, 2D, 3D, and so on. Here's a tutorial on the basics of array operations in R:
To create an array, you can use the array()
function. Suppose you want a 2x3x2 array:
data <- 1:12 array1 <- array(data, dim=c(2, 3, 2)) print(array1)
Here, dim
specifies the dimensions of the array, so 2x3x2
means two 2x3 matrices.
Just like vectors and matrices, you can perform element-wise arithmetic on arrays:
array2 <- array(12:1, dim=c(2, 3, 2)) result <- array1 + array2 print(result)
Indexing into arrays is an extension of matrix indexing:
array[i]
array[i, j]
array[i, j, k]
Example:
print(array1[1, 2, 1]) # Retrieves the value in the 1st row, 2nd column of the 1st matrix
The apply()
function is useful when you want to apply a function (like sum or mean) across margins of an array:
# Sum across rows (1st margin) of each matrix: row_sums <- apply(array1, MARGIN=1, FUN=sum) # Sum across columns (2nd margin) of each matrix: col_sums <- apply(array1, MARGIN=2, FUN=sum) # Sum across matrices (3rd margin): mat_sums <- apply(array1, MARGIN=3, FUN=sum)
You can combine arrays using the abind()
function from the abind
package:
# Installing and loading the abind package install.packages("abind") library(abind) array3 <- abind(array1, array2, along=3) # Combines along the third dimension
You can assign names to the dimensions of your array:
dimnames(array1) <- list( c("Row1", "Row2"), c("Col1", "Col2", "Col3"), c("Matrix1", "Matrix2") ) print(array1)
You can convert an array to a matrix if it's a 2D array:
mat <- matrix(data, nrow=2)
Converting a matrix to an array is technically redundant because a matrix is just a 2D array. However, you can extend a matrix into a higher-dimensional array using the array()
function and specifying new dimensions.
Arrays in R are a powerful data structure, especially for analyses requiring multi-dimensional data. They can be thought of as generalized matrices. With the operations and functions discussed, you can manipulate and analyze multi-dimensional data effectively in R.
R Array Operations Example:
# Create a 2x3x4 array my_array <- array(1:24, dim = c(2, 3, 4)) # Display the array print(my_array)
How to Create Arrays in R:
# Create a 3x3 matrix and convert it to an array my_matrix <- matrix(1:9, nrow = 3) my_array <- array(my_matrix, dim = c(3, 3, 1))
Indexing and Subsetting Arrays in R:
# Create a 2x3 array my_array <- array(1:6, dim = c(2, 3)) # Access elements using indices element <- my_array[1, 2] # Access element at row 1, column 2
Array Manipulation in R:
# Create a 2x3 array my_array <- array(1:6, dim = c(2, 3)) # Modify elements of the array my_array[1, 2] <- 10
Applying Functions to Arrays in R:
# Create a 2x3 array my_array <- array(1:6, dim = c(2, 3)) # Apply a function to each element result <- apply(my_array, c(1, 2), function(x) x * 2)
Mathematical Operations on Arrays in R:
# Create two 2x3 arrays array1 <- array(1:6, dim = c(2, 3)) array2 <- array(7:12, dim = c(2, 3)) # Perform element-wise addition result <- array1 + array2
Reshaping Arrays in R:
# Create a 2x3 array my_array <- array(1:6, dim = c(2, 3)) # Reshape the array to a 3x2 matrix reshaped_array <- matrix(my_array, nrow = 3)
Array Operations with Apply Functions in R:
# Create a 2x3 array my_array <- array(1:6, dim = c(2, 3)) # Apply a function along a specific margin row_sums <- apply(my_array, 1, sum)
Multidimensional Arrays in R:
# Create a 2x3x4 array my_array <- array(1:24, dim = c(2, 3, 4))
Vectorized Operations on Arrays in R:
# Create a 2x3 array my_array <- array(1:6, dim = c(2, 3)) # Perform vectorized operation squared_array <- my_array^2