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
In R, arrays can be used to represent multi-dimensional datasets. While matrices in R are limited to two dimensions (rows and columns), arrays can have two or more dimensions. This tutorial will guide you through the creation, indexing, and manipulation of multidimensional arrays in R.
You can create an array in R using the array()
function:
# Create a 3x3x2 array data <- 1:18 # Data from 1 to 18 array_dims <- c(3, 3, 2) my_array <- array(data, dim = array_dims) print(my_array)
Here, my_array
is a 3x3 matrix repeated in 2 layers.
Indexing an array is similar to indexing a matrix, but you have additional dimensions to consider:
# Accessing an element element <- my_array[2, 3, 1] # Fetches the element in the 2nd row, 3rd column, and 1st layer # Accessing a whole layer layer1 <- my_array[, , 1] # Accessing a specific row across layers row1_across_layers <- my_array[1, , ]
You can perform operations on arrays just like you would with matrices:
# Create another 3x3x2 array data2 <- 19:36 array2 <- array(data2, dim = array_dims) # Sum of two arrays sum_array <- my_array + array2 # Element-wise multiplication product_array <- my_array * array2
The apply()
function allows you to apply a function (like sum, mean) across the margins of an array:
# Sum across the 1st margin (rows) row_sums <- apply(my_array, MARGIN = 1, FUN = sum) # Average across the 2nd margin (columns) col_means <- apply(my_array, MARGIN = 2, FUN = mean)
You can combine multidimensional arrays using functions like abind
from the abind
package:
install.packages("abind") library(abind) # Combining arrays along the third dimension combined_array <- abind(my_array, array2, along = 3)
Multidimensional arrays in R are a flexible data structure that allows you to represent and manipulate complex datasets. While they are powerful, they can also become challenging to visualize and conceptualize as the number of dimensions increases. In such cases, alternative data structures or specialized packages may be more appropriate.
Creating multidimensional arrays in R:
Overview: Introduce the concept of multidimensional arrays in R and how to create them.
Code:
# Creating a 3D array in R arr <- array(1:27, dim = c(3, 3, 3)) print("3D Array:") print(arr)
Indexing and slicing multidimensional arrays in R:
Overview: Explain how to index and slice multidimensional arrays to access specific elements or subsets.
Code:
# Indexing and slicing a 3D array in R arr <- array(1:27, dim = c(3, 3, 3)) # Accessing an element print("Element at (2, 2, 2):") print(arr[2, 2, 2]) # Slicing along dimensions print("Slicing along the third dimension:") print(arr[,, 3])
R apply function on multidimensional arrays:
Overview: Demonstrate how to use the apply()
function on multidimensional arrays for applying a function along specific dimensions.
Code:
# Applying a function along a dimension in a 3D array arr <- array(1:27, dim = c(3, 3, 3)) # Applying sum along the third dimension summed_array <- apply(arr, 3, sum) print("Original 3D Array:") print(arr) print("Summed along the third dimension:") print(summed_array)