Higher-Dimensional Arrays
Creating Higher-Dimensional Arrays
Higher-dimensional arrays in R can be created using the array() function, which allows you to specify the dimensions and optionally names for each dimension.
Example 1: Creating a 3D Array
# Creating a 3D array with dimensions 2x3x4 arr <- array(1:24, dim = c(2, 3, 4), dimnames = list(c("Layer1", "Layer2"), c("Row1", "Row2", "Row3"), c("Col1", "Col2", "Col3", "Col4"))) print(arr) # The output will be: # , , Col1 # Col1 Col2 Col3 Col4 # Layer1 1 5 9 13 # Layer2 2 6 10 14 # , , Col2 # Col1 Col2 Col3 Col4 # Layer1 3 7 11 15 # Layer2 4 8 12 16 # , , Col3 # Col1 Col2 Col3 Col4 # Layer1 5 9 13 17 # Layer2 6 10 14 18 # , , Col4 # Col1 Col2 Col3 Col4 # Layer1 7 11 15 19 # Layer2 8 12 16 20
Accessing Elements in Higher-Dimensional Arrays
You can access elements in a higher-dimensional array by specifying indices for each dimension.
Example 2: Accessing Elements
# Accessing the element in the second layer, first row, third column element <- arr[2, 1, 3] print(element) # Outputs 10 # Accessing the entire second layer layer2 <- arr[2, , ] print(layer2) # The output will be: # Col1 Col2 Col3 Col4 # Row1 2 6 10 14 # Row2 4 8 12 16
Modifying Elements in Higher-Dimensional Arrays
You can modify elements by directly assigning new values using indices.
Example 3: Modifying Elements
# Modifying an element in the array arr[1, 2, 4] <- 99 print(arr[1, , ]) # The output will be: # Col1 Col2 Col3 Col4 # Row1 1 5 9 99 # Row2 2 6 10 14
Using apply() with Higher-Dimensional Arrays
The apply() function can be used to perform operations over one or more dimensions of a higher-dimensional array.
Example 4: Using apply()
# Summing over the third dimension (columns) sum_layers <- apply(arr, c(1, 2), sum) print(sum_layers) # The output will be: # Col1 Col2 Col3 Col4 # Row1 10 22 34 46 # Row2 12 24 36 48
Using sweep() with Higher-Dimensional Arrays
The sweep() function can be used to perform operations with a margin array, useful for subtracting or adding values across a dimension.
Example 5: Using sweep()
# Creating a matrix to subtract from each layer subtract_matrix <- matrix(c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3), nrow = 3) # Subtracting a matrix from each layer result <- sweep(arr, 2, subtract_matrix, "-") print(result)
Reshaping Higher-Dimensional Arrays
You can reshape arrays using functions like aperm() to permute array dimensions and array() to change dimensions.
Example 6: Using aperm()
# Permuting dimensions of the array perm_arr <- aperm(arr, c(3, 1, 2)) print(perm_arr) # The output will be: # , , Layer1 # Row1 Row2 # Col1 1 2 # Col2 5 6 # Col3 9 10 # Col4 13 14 # , , Layer2 # Row1 Row2 # Col1 3 4 # Col2 7 8 # Col3 11 12 # Col4 15 16
Example 7: Reshaping with array()
# Reshaping the array to dimensions 4x3x2 reshaped_arr <- array(arr, dim = c(4, 3, 2)) print(reshaped_arr) The output will show the array reshaped into the new dimensions.
Naming Dimensions
Just like with matrices, you can name the dimensions of higher-dimensional arrays for better readability.
Example 8: Naming Dimensions
# Creating a 3D array with named dimensions arr <- array(1:24, dim = c(2, 3, 4), dimnames = list(Layers = c("Layer1", "Layer2"), Rows = c("Row1", "Row2", "Row3"), Columns = c("Col1", "Col2", "Col3", "Col4"))) print(arr)
Summary
- Creating Arrays: Use array() to create higher-dimensional arrays with specified dimensions.
- Accessing Elements: Access specific elements by specifying indices for each dimension.
- Modifying Elements: Directly modify elements using indices.
- Applying Functions: Use apply() to perform operations over dimensions, and sweep() for margin-based operations.
- Reshaping Arrays: Use aperm() to permute dimensions and array() to reshape arrays.
- Naming Dimensions: Assign names to dimensions for improved clarity.
Higher-dimensional arrays in R provide powerful capabilities for managing and analyzing multi-dimensional data, allowing for sophisticated data manipulation and analysis.