Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

10/26/23, 10:53 AM 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.

#create a 3x3x2 array


data_vector <- 1:18
dim_vector <- c(3,3,2)
my_array <- array(data_vector, dim=dim_vector)

#Print the array


my_array

, , 1

[,1] [,2] [,3]


[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

, , 2

[,1] [,2] [,3]


[1,] 10 13 16
[2,] 11 14 17
[3,] 12 15 18

#Access an element - 2nd row, 3rd column, 1st slice


my_array[2,3,1]

[1] 8

https://sta334.s3.ap-southeast-1.amazonaw s.com/day3/Array+in+R.html 1/8


10/26/23, 10:53 AM Array

#Access an element - 3rd row, 2nd column, 2nd slice


my_array[3,2,2]

[1] 15

Array Arithmetic

Array creation

# Create two 2x3x2 arrays


array1 <- array(1:12, dim=c(2,3,2))
array2 <- array(13:24, dim=c(2,3,2))

array1 ; array2

, , 1

[,1] [,2] [,3]


[1,] 1 3 5
[2,] 2 4 6

, , 2

[,1] [,2] [,3]


[1,] 7 9 11
[2,] 8 10 12

, , 1

[,1] [,2] [,3]


[1,] 13 15 17
[2,] 14 16 18

, , 2

[,1] [,2] [,3]


[1,] 19 21 23
[2,] 20 22 24

Array Addition
You can add arrays of the same dimension element-wise:

array1 + array2

, , 1

[,1] [,2] [,3]


[1,] 14 18 22
[2,] 16 20 24

https://sta334.s3.ap-southeast-1.amazonaw s.com/day3/Array+in+R.html 2/8


10/26/23, 10:53 AM Array
, , 2

[,1] [,2] [,3]


[1,] 26 30 34
[2,] 28 32 36

Array subtraction
Similarly, arrays can be subtracted element-wise:

array1 - array2

, , 1

[,1] [,2] [,3]


[1,] -12 -12 -12
[2,] -12 -12 -12

, , 2

[,1] [,2] [,3]


[1,] -12 -12 -12
[2,] -12 -12 -12

Element-wise Multiplication
This will multiply corresponding elements in the two arrays:

array1 * array2

, , 1

[,1] [,2] [,3]


[1,] 13 45 85
[2,] 28 64 108

, , 2

[,1] [,2] [,3]


[1,] 133 189 253
[2,] 160 220 288

Scalar Operations
You can perform scalar operations on arrays as well:

array1 * 2

, , 1

[,1] [,2] [,3]


[1,] 2 6 10
[2,] 4 8 12

https://sta334.s3.ap-southeast-1.amazonaw s.com/day3/Array+in+R.html 3/8


10/26/23, 10:53 AM Array

, , 2

[,1] [,2] [,3]


[1,] 14 18 22
[2,] 16 20 24

Array Division
Element-wise division can be performed if none of the elements in the divisor array are zero:

array1 / array2

, , 1

[,1] [,2] [,3]


[1,] 0.07692308 0.20 0.2941176
[2,] 0.14285714 0.25 0.3333333

, , 2

[,1] [,2] [,3]


[1,] 0.3684211 0.4285714 0.4782609
[2,] 0.4000000 0.4545455 0.5000000

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:

# Swap the first and second dimensions of array1


aperm(array1, c(2,1,3))

, , 1

[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6

, , 2

[,1] [,2]
[1,] 7 8
[2,] 9 10
[3,] 11 12

Applying Functions to Arrays


Just like matrices, you can apply functions over margins (dimensions) of an array using the apply()
function:

https://sta334.s3.ap-southeast-1.amazonaw s.com/day3/Array+in+R.html 4/8


10/26/23, 10:53 AM Array

# 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”.

FUN : The function to be applied.

... : Additional arguments for the function FUN .

Examples:

1. Sum Over Array Margins:


Consider a 3D array:

A <- array(1:24, dim=c(2,3,4))


print(A)

, , 1

[,1] [,2] [,3]


[1,] 1 3 5
[2,] 2 4 6

, , 2

[,1] [,2] [,3]


[1,] 7 9 11
[2,] 8 10 12

, , 3

[,1] [,2] [,3]


[1,] 13 15 17
[2,] 14 16 18

https://sta334.s3.ap-southeast-1.amazonaw s.com/day3/Array+in+R.html 5/8


10/26/23, 10:53 AM Array

, , 4

[,1] [,2] [,3]


[1,] 19 21 23
[2,] 20 22 24
If we want to sum over the 1st dimension (rows), we use:

apply(A, MARGIN=1, FUN=sum)

[1] 144 156

For summing over the 2nd dimension (columns):

apply(A, MARGIN=2, FUN=sum)

[1] 84 100 116

And for the 3rd dimension (sum for each dimension):

apply(A, MARGIN=3, FUN=sum)

[1] 21 57 93 129

2. Applying Multiple Margins:


If you want to apply the function over multiple dimensions simultaneously, you can provide a vector to
the MARGIN argument.

For example, to get the sum over both rows and columns, leaving the third dimension:

apply(A, MARGIN=c(1,2), FUN=sum)

[,1] [,2] [,3]


[1,] 40 48 56
[2,] 44 52 60

3. Using Custom Functions:


You’re not limited to built-in functions. You can define your own functions and use them with apply() .

For instance, to get the range (difference between max and min) of each column:

range_function <- function(x) {


return(max(x) - min(x))
}

column_ranges <- apply(A, MARGIN=2, FUN=range_function)


print(column_ranges)

[1] 19 19 19

https://sta334.s3.ap-southeast-1.amazonaw s.com/day3/Array+in+R.html 6/8


10/26/23, 10:53 AM Array

4. Additional Arguments:
If your function requires additional arguments, you can pass them after specifying the function (by
rows):

# Example: raising elements to a specific power


power_function <- function(x, p) {
return(x^p)
}

# Apply the power function with p=3


cubed_values <- apply(A, MARGIN=1, FUN=power_function, p=3)
print(cubed_values)

[,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?

Exercise 2: Array Arithmetic

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 .

3. Multiply my_array by a scalar value of 2. Print the result.

https://sta334.s3.ap-southeast-1.amazonaw s.com/day3/Array+in+R.html 7/8


10/26/23, 10:53 AM Array

4. Execute element-wise multiplication between my_array and another_array . Print the outcome.

Exercise 3: Using apply() with Arrays

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

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.

Exercise 4: Advanced apply() Usage

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.

https://sta334.s3.ap-southeast-1.amazonaw s.com/day3/Array+in+R.html 8/8

You might also like