Open In App

Performing Operations on Multiple Lists simultaneously in R Programming – mapply() Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

mapply() function in R Language is stand for multivariate apply and is used to perform mathematical operations on multiple lists simultaneously. 

Syntax: mapply(func, list1, list2, …)

Parameters: 

  • list1, list2, …: Created Lists
  • func: operation to be applied

mapply() Function in R Language Example

Example 1: Sum of two lists using mapply() function in R

R




# R program to illustrate
# mapply() function
   
# Creating a list
A = list(c(1, 2, 3, 4))
   
# Creating another list
B = list(c(2, 5, 1, 6))
   
# Applying mapply()
result = mapply(sum, A, B)
print(result)


Output: 

[1] 24

Example 2: Product of two lists using mapply() function in R

R




# R program to illustrate
# mapply() function
   
# Creating a list
A = list(c(1, 2, 3, 4))
   
# Creating another list
B = list(c(2, 5, 1, 6))
   
# Applying mapply()
result = mapply(prod, A, B)
print(result)


Output: 

[1] 1440

Example 3: mapply() function with multiple arguments

R




Data1 <- c(1,2,3)
Data2 <- c(10,20,30)
mult_one<-function(Data1,Data2)
{
   Data1+Data2
}
mapply(mult_one,Data1,Data2)


Output:

11 22 33


Last Updated : 08 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads