Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

function
r
communitycreator

What is the mapply() function in R?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The mapply() function in R is used to apply a function FUN to a given list or a vector argument passed to it.

Syntax

mapply(FUN, …, MoreArgs = NULL, SIMPLIFY = TRUE,
USE.NAMES = TRUE)
Syntax for the mapply() function in R

Parameter value

The mapply() function takes the following parameter values:

  • FUN: This is the function to apply to the provided vector arguments. It is a required parameter.
  • ...: This is a list of the arguments to which the FUN is applied. It is a required parameter.
  • MoreArgs: This is a list of other vector arguments to FUN. It is an optional parameter.
  • SIMPLIFY : This takes a logical value (TRUE or FALSE) indicating whether the result is reduced to a vector, matrix, or an array of higher dimensions. It is an optional parameter.
  • USE.NAMES: This takes a logical value indicating whether to use the name if the first argument of the function has names, or use a character vector if the argument is a character vector. It is an optional parameter.

Return value

The mapply() function returns a list or if the argument, SIMPLICITY = TRUE, a vector, array, or list.

Code example

Let's look at the code below:

# creating a function
myfunction <- function(a, b, c) {
a*b + c
}
# creating a list of vector arguments
a = c(1, 4, 0, 6)
b = c(1, 2, 3, 0)
c = c(5, 0, 4, 3)
# applying the function over the list of arguments
mapply(myfunction, a, b, c)
Implementing the mapply() function

Code explanation

  • Line 2: We create a function, myfunction, using the function() function.
  • Lines 7 to 9: We create a list of vector arguments, a, b, and c .
  • Line 12: We implement the mapply() function on the function myfunction and the list of vector arguments provided. The result is printed to the console.

RELATED TAGS

function
r
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring