R Tutorial

Fundamentals of R

Variables

Input and Output

Decision Making

Control Flow

Functions

Strings

Vectors

Lists

Arrays

Matrices

Factors

DataFrames

Object Oriented Programming

Error Handling

File Handling

Packages in R

Data Interfaces

Data Visualization

Statistics

Machine Learning with R

Arrays in R

Arrays in R are a multi-dimensional data structure that can hold more than two dimensions. Essentially, if matrices are 2D (rows x columns), arrays can be 2D, 3D, 4D, and so on. They are useful for operations that need to be performed across several different dimensions of data.

Here's a tutorial on arrays in R:

1. Creating Arrays

You can create an array using the array() function:

# Create a 3x3x2 array
arr <- array(1:18, dim = c(3, 3, 2))
print(arr)

In the above code, we're creating an array with the numbers 1 through 18 and specifying its dimensions to be 3x3x2.

2. Accessing Elements in an Array

Elements in an array can be accessed using the indices for each dimension:

# Access the element in the 1st row, 2nd column of the 2nd matrix
arr[1, 2, 2]

3. Operations on Arrays

Arrays support typical arithmetic operations:

arr1 <- array(1:6, dim = c(2, 3))
arr2 <- array(7:12, dim = c(2, 3))

# Array addition
result <- arr1 + arr2
print(result)

4. Applying Functions to Array Margins

The apply() function can be used to apply a function across margins of an array:

# Sum across the rows (1st margin) of each matrix in the array
row_sums <- apply(arr, MARGIN = 1, FUN = sum)

# Sum across the columns (2nd margin) of each matrix in the array
col_sums <- apply(arr, MARGIN = 2, FUN = sum)

5. Combining Arrays

The abind() function from the abind package is helpful for combining arrays:

# Install and load abind package
install.packages("abind")
library(abind)

arr3 <- array(19:24, dim = c(2, 3))
combined <- abind(arr1, arr2, arr3, along = 3)  # Stack along the third dimension

6. Naming Array Dimensions

You can assign names to the dimensions of an array:

# Naming the dimensions
rownames <- c("R1", "R2", "R3")
colnames <- c("C1", "C2", "C3")
matnames <- c("M1", "M2")

arr <- array(1:18, dim = c(3, 3, 2), dimnames = list(rownames, colnames, matnames))
print(arr)

7. Checking if an Object is an Array

The is.array() function can be used to check if an object is an array:

is.array(arr1)  # Returns TRUE or FALSE

Conclusion

Arrays in R offer a flexible and powerful way to handle multi-dimensional data. While they might seem more complex than vectors or matrices, they are essential when working with higher-dimensional data. Functions like apply() and packages like abind further augment the usability of arrays in various applications.

  1. Creating and initializing arrays in R:

    # Creating a vector
    vec <- c(1, 2, 3, 4)
    
    # Creating a matrix
    mat <- matrix(1:6, nrow = 2, ncol = 3)
    
    # Creating a 3D array
    arr <- array(1:24, dim = c(2, 3, 4))
    
  2. Manipulating arrays with apply functions in R:

    # Applying a function to each column of a matrix
    apply(mat, 2, mean)
    
    # Applying a function to each row of a matrix
    apply(mat, 1, sum)
    
    # Applying a function to each element of a 3D array
    apply(arr, c(1, 2), sum)
    
  3. Indexing and slicing arrays in R:

    # Indexing a matrix
    mat[1, 2]
    
    # Slicing a matrix
    mat[, 1:2]
    
    # Indexing a 3D array
    arr[1, 2, 3]
    
  4. R array operations and arithmetic:

    # Element-wise addition of two arrays
    arr1 <- array(1:24, dim = c(2, 3, 4))
    arr2 <- array(25:48, dim = c(2, 3, 4))
    arr_sum <- arr1 + arr2
    
  5. Converting matrices to arrays in R:

    # Converting a matrix to an array
    mat <- matrix(1:6, nrow = 2, ncol = 3)
    arr_from_mat <- array(mat, dim = c(2, 3, 1))
    
  6. Reshaping data into arrays in R:

    # Reshaping a vector into a matrix
    vec <- 1:6
    mat_from_vec <- matrix(vec, nrow = 2, ncol = 3)