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
The mapply()
function in R is a multivariate version of the sapply()
function. It applies a function to two or more lists or vectors in a pairwise fashion. It's especially useful when you want to apply a function that requires multiple arguments, and you want to pass these arguments from different lists or vectors.
mapply()
function:Syntax:
mapply(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE)
FUN
: The function to be applied....
: Arguments to FUN
, which are typically lists or vectors.MoreArgs
: A list of other arguments to FUN
.SIMPLIFY
: Logical. Should the result be simplified to a vector, matrix, or higher-dimensional array if possible?USE.NAMES
: Logical. Should names be preserved?Suppose we have two vectors, and we want to add their corresponding elements:
A <- c(1, 2, 3) B <- c(4, 5, 6) result <- mapply(sum, A, B) print(result) # [1] 5 7 9
Imagine we want to calculate the power of each element from vector A raised to the corresponding element of vector B:
A <- c(2, 3, 4) B <- c(3, 2, 1) result <- mapply(function(x, y) x^y, A, B) print(result) # [1] 8 9 4
mapply()
can work with more than two lists. For instance, let's calculate the sum of elements from three vectors:
A <- c(1, 2, 3) B <- c(4, 5, 6) C <- c(7, 8, 9) result <- mapply(sum, A, B, C) print(result) # [1] 12 15 18
MoreArgs
:If a function requires more arguments, and some of them are constant, you can use MoreArgs
:
# A function that multiplies two numbers and then adds a constant myfunc <- function(x, y, const) { return(x * y + const) } A <- c(2, 4, 6) B <- c(3, 5, 7) result <- mapply(myfunc, A, B, MoreArgs = list(const = 10)) print(result) # [1] 16 30 52
By default, mapply()
tries to simplify the result to a vector or matrix. If you want to preserve the list format, set SIMPLIFY
to FALSE
:
A <- c(2, 4) B <- c(3, 5) result <- mapply(function(x, y) c(sum = x + y, diff = x - y), A, B, SIMPLIFY = FALSE) print(result)
The mapply()
function is a powerful tool in R that allows for the application of functions over multiple lists or vectors simultaneously. Its flexibility in handling multiple arguments and its options for result simplification make it a useful function to have in your R toolbox.
Simultaneous operations on multiple lists in R:
Overview: Demonstrate performing operations on corresponding elements of multiple lists.
Code:
# Simultaneous operations on multiple lists list1 <- c(1, 2, 3) list2 <- c(4, 5, 6) # Element-wise addition sum_list <- list1 + list2 # Printing the result print("List1:") print(list1) print("List2:") print(list2) print("Element-wise Addition:") print(sum_list)
Using mapply() for element-wise operations in R:
Overview: Introduce the mapply()
function for element-wise operations on multiple lists.
Code:
# Using mapply() for element-wise operations list1 <- c(1, 2, 3) list2 <- c(4, 5, 6) # Element-wise addition using mapply sum_list <- mapply(FUN = function(x, y) x + y, list1, list2) # Printing the result print("List1:") print(list1) print("List2:") print(list2) print("Element-wise Addition using mapply:") print(sum_list)
Applying functions to multiple lists with mapply() in R:
Overview: Demonstrate applying custom functions to corresponding elements of multiple lists using mapply()
.
Code:
# Applying functions to multiple lists with mapply() list1 <- c(1, 2, 3) list2 <- c(4, 5, 6) # Custom function: product of squares custom_function <- function(x, y) x^2 * y # Applying custom function using mapply result <- mapply(FUN = custom_function, list1, list2) # Printing the result print("Result of Custom Function:") print(result)
Vectorized operations on lists using mapply() in R:
Overview: Showcase vectorized operations on lists using mapply()
for efficiency.
Code:
# Vectorized operations on lists using mapply() list1 <- c(1, 2, 3) list2 <- c(4, 5, 6) # Vectorized division using mapply division_result <- mapply("/", list1, list2) # Printing the result print("List1:") print(list1) print("List2:") print(list2) print("Vectorized Division using mapply:") print(division_result)
R mapply() with custom functions for list manipulation:
Overview: Demonstrate using mapply()
with custom functions for advanced list manipulation.
Code:
# mapply() with custom functions for list manipulation list1 <- c(1, 2, 3) list2 <- c(4, 5, 6) # Custom function: concatenate and square custom_function <- function(x, y) c(x, y)^2 # Applying custom function using mapply result <- mapply(FUN = custom_function, list1, list2) # Printing the result print("Result of Custom Function:") print(result)
Parallel processing with mapply() in R programming:
Overview: Introduce the concept of parallel processing using mapply()
for increased efficiency.
Code: Provide examples and mention the mc.cores
parameter.
# Parallel processing with mapply() list1 <- c(1, 2, 3) list2 <- c(4, 5, 6) # Custom function: product of squares custom_function <- function(x, y) x^2 * y # Applying custom function using mapply with parallel processing result <- mapply(FUN = custom_function, list1, list2, SIMPLIFY = FALSE, mc.cores = 2) # Printing the result print("Result of Custom Function with Parallel Processing:") print(result)