R - Arrays

In R, an array is a multi-dimensional data structure that can hold elements of the same data type. Arrays are used to store data with more than two dimensions, making them useful for working with complex data structures such as images, scientific data, and tensors. Arrays are created using the array() function. The array() function takes a list of values as its argument and returns an array. The array() function also takes the dimensions of the array as its arguments.

Creating Arrays

You can create an array in R using the array() function, where you provide the data, dimensions, and optionally names for dimensions.

Creating a 2-D Array
data_2d <- c(1, 2, 3, 4, 5, 6) array_2d <- array(data = data_2d, dim = c(2, 3)) #Output: [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6
Creating a 3-D Array
data_3d <- 1:24 array_3d <- array(data = data_3d, dim = c(2, 3, 4))
#Output: , , 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 , , 4 [,1] [,2] [,3] [1,] 19 21 23 [2,] 20 22 24

Array Operations

Arrays support various element-wise and matrix operations, depending on the dimensions.

A <- array(data = c(1, 2, 3, 4), dim = c(2, 2)) B <- array(data = c(5, 6, 7, 8), dim = c(2, 2)) sum_array <- A + B # Element-wise addition #Output: [,1] [,2] [1,] 6 10 [2,] 8 12

Indexing and Subsetting

You can access specific elements or subsets of an array using indexing.

data_array <- array(data = 1:27, dim = c(3, 3, 3)) element <- data_array[2, 1, 3] # Accessing the element at (2, 1, 3) subset_array <- data_array[,, 2] # Subsetting along the third dimension #Output: [,1] [,2] [,3] [1,] 10 13 16 [2,] 11 14 17 [3,] 12 15 18

Array Functions

R provides functions for working with arrays, such as reshaping and applying operations along specific dimensions.

Reshaping Arrays
reshaped_array <- array(data = 1:12, dim = c(3, 2, 2)) matrix_form <- matrix(reshaped_array, nrow = 3, byrow = TRUE) #Output: [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 5 6 7 8 [3,] 9 10 11 12

The sliced_array array now contains the first row of the array_of_numbers array.

Points to remember:

  1. Arrays can be used to represent data that is naturally multi-dimensional, such as a 3D image or a 4D tensor.
  2. Arrays can be used to perform mathematical operations on data, such as adding, subtracting, multiplying, and dividing.
  3. Arrays can be used to sort and filter data.

Conclusion

Arrays are indispensable when dealing with data that has more than two dimensions. They are essential for scientific computing, image processing, and other scenarios where data has complex structures. Understanding arrays enables you to efficiently manipulate and analyze multi-dimensional data within the R programming environment.