While matrices are restricted to two dimensions (rows and columns), arrays break this barrier and can have any number of dimensions. This versatility makes arrays an essential tool for complex data storage and manipulation, particularly when working with data that inherently possesses multiple dimensions, such as time series data for multiple subjects across various conditions.
Understanding Arrays
Dimensions: The key feature that differentiates arrays from matrices is the ability to have more than two dimensions. Each dimension in an array can be thought of as a different “mode” of the data.
Creation: Arrays are created using the array() function. While the data is often supplied as a vector, the shape of the array is defined using the dim argument, which takes a vector of dimension lengths.
Indexing: Accessing elements within an array requires an index for each dimension. For a three-dimensional array, for instance, you’d need three indices to access a specific element.
#create a 3x3x2 arraydata_vector <-1:18dim_vector <-c(3,3,2)my_array <-array(data_vector, dim=dim_vector)#Print the arraymy_array
While we often transpose matrices, transposing multi-dimensional arrays can be more complex. However, the aperm() function can be used to permute array dimensions:
# Swap the first and second dimensions of array1aperm(array1, c(2,1,3))
Just like matrices, you can apply functions over margins (dimensions) of an array using the apply() function:
# Calculate the sum of the elements over the third dimension (margin=3)apply(array1, MARGIN=3, sum)
[1] 21 57
Further Discussion on Apply in Array
The apply() function is a powerful tool in R, designed for applying a function to the margins of an array or matrix. In the context of arrays, “margins” refer to the dimensions. By selecting a margin, you essentially decide along which dimension the function will be applied.
Basics
The syntax for the apply() function is:
apply(X, MARGIN, FUN, ...)
X: The array or matrix.
MARGIN: An integer vector indicating which margins should be “retained”.
If your function requires additional arguments, you can pass them after specifying the function (by rows):
# Example: raising elements to a specific powerpower_function <-function(x, p) {return(x^p)}# Apply the power function with p=3cubed_values <-apply(A, MARGIN=1, FUN=power_function, p=3)print(cubed_values)
The apply() function offers a versatile way to perform operations along specific dimensions of an array without the need for explicit loops. This often results in more concise and faster code. Understanding how to use apply() effectively can significantly streamline data processing and analysis tasks in R.
Exercise
Exercise 1: Basic Array Operations
Create a 3D array named my_array with dimensions 3x4x2 using numbers from 1 to 24. Print the array.
Access and print the element located in the 2nd row, 3rd column, and 1st layer of my_array.
Retrieve the entire 1st layer of my_array. What values are present?
Exercise 2: Array Arithmetic
Create another 3D array named another_array with dimensions 3x4x2 using numbers from 25 to 48.
Perform and print the result of the element-wise addition of my_array and another_array.
Multiply my_array by a scalar value of 2. Print the result.
Execute element-wise multiplication between my_array and another_array. Print the outcome.
Exercise 3: Using apply() with Arrays
Using my_array from Exercise 1:
a. Calculate and print the sum of elements along the 1st dimension (rows). b. Compute and print the mean value for each layer (3rd dimension). c. Determine the maximum value for each column across all layers. Print the results.
Define a function that calculates the range (difference between maximum and minimum) of a numeric vector.
a. Use the apply() function to calculate the range for each column in my_array across all layers. Print the results. b. Modify the function to also return the mean of the vector. Use apply() to retrieve both the range and mean for each row in my_array across all layers. Print the outcomes.
Exercise 4: Advanced apply() Usage
Using my_array:
a. Define a custom function that multiplies a vector by a given scalar and then adds another scalar (both scalars are arguments to the function). b. Use the apply() function to apply this custom function on my_array, choosing a multiplication scalar of 0.5 and an addition scalar of 10. Print the result.
Calculate the standard deviation for each row in my_array across all columns and layers.