Professional Documents
Culture Documents
Array
Array
Array
AUTHOR
Dr. Mohammad Nasir Abdullah
Array
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
1. 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.
2. 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.
3. 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.
, , 1
, , 2
[1] 8
[1] 15
Array Arithmetic
Array creation
array1 ; array2
, , 1
, , 2
, , 1
, , 2
Array Addition
You can add arrays of the same dimension element-wise:
array1 + array2
, , 1
Array subtraction
Similarly, arrays can be subtracted element-wise:
array1 - array2
, , 1
, , 2
Element-wise Multiplication
This will multiply corresponding elements in the two arrays:
array1 * array2
, , 1
, , 2
Scalar Operations
You can perform scalar operations on arrays as well:
array1 * 2
, , 1
, , 2
Array Division
Element-wise division can be performed if none of the elements in the divisor array are zero:
array1 / array2
, , 1
, , 2
Transposing an 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:
, , 1
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
, , 2
[,1] [,2]
[1,] 7 8
[2,] 9 10
[3,] 11 12
# Calculate the sum of the elements over the third dimension (margin=3)
apply(array1, MARGIN=3, sum)
[1] 21 57
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:
Examples:
, , 1
, , 2
, , 3
, , 4
[1] 21 57 93 129
For example, to get the sum over both rows and columns, leaving the third dimension:
For instance, to get the range (difference between max and min) of each column:
[1] 19 19 19
4. Additional Arguments:
If your function requires additional arguments, you can pass them after specifying the function (by
rows):
[,1] [,2]
[1,] 1 8
[2,] 27 64
[3,] 125 216
[4,] 343 512
[5,] 729 1000
[6,] 1331 1728
[7,] 2197 2744
[8,] 3375 4096
[9,] 4913 5832
[10,] 6859 8000
[11,] 9261 10648
[12,] 12167 13824
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
1. Create a 3D array named my_array with dimensions 3x4x2 using numbers from 1 to 24. Print the
array.
2. Access and print the element located in the 2nd row, 3rd column, and 1st layer of my_array .
3. Retrieve the entire 1st layer of my_array . What values are present?
1. Create another 3D array named another_array with dimensions 3x4x2 using numbers from 25 to
48.
2. Perform and print the result of the element-wise addition of my_array and another_array .
4. Execute element-wise multiplication between my_array and another_array . Print the outcome.
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.
2. 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.
1. 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.
2. Calculate the standard deviation for each row in my_array across all columns and layers.