mapply {base} R Documentation

## Apply a Function to Multiple List or Vector Arguments

### Description

mapply is a multivariate version of sapply. mapply applies FUN to the first elements of each ... argument, the second elements, the third elements, and so on. Arguments are recycled if necessary.

.mapply() is a bare-bones version of mapply(), e.g., to be used in other functions.

### Usage

mapply(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE,
USE.NAMES = TRUE)
.mapply(FUN, dots, MoreArgs)


### Arguments

 FUN function to apply, found via match.fun. ... arguments to vectorize over, will be recycled to common length (zero if one of them is). See also ‘Details’. dots list or pairlist of arguments to vectorize over, see ... above. MoreArgs a list of other arguments to FUN. SIMPLIFY logical or character string; attempt to reduce the result to a vector, matrix or higher dimensional array; see the simplify argument of sapply. USE.NAMES logical; use the names of the first ... argument, or if that is an unnamed character vector, use that vector as the names.

### Details

mapply calls FUN for the values of ... (re-cycled to the length of the longest, unless any have length zero where recycling to zero length will return list()), followed by the arguments given in MoreArgs. The arguments in the call will be named if ... or MoreArgs are named.

For the arguments in ... (or components in dots) class specific subsetting (such as [) and length methods will be used where applicable.

### Value

A list, or for SIMPLIFY = TRUE, a vector, array or list.

sapply, after which mapply() is modelled.

outer, which applies a vectorized function to all combinations of two arguments.

### Examples

mapply(rep, 1:4, 4:1)

mapply(rep, times = 1:4, x = 4:1)

mapply(rep, times = 1:4, MoreArgs = list(x = 42))

mapply(function(x, y) seq_len(x) + y,
c(a =  1, b = 2, c = 3),  # names from first
c(A = 10, B = 0, C = -10))

word <- function(C, k) paste(rep.int(C, k), collapse = "")
## names from the first, too:
utils::str(L <- mapply(word, LETTERS[1:6], 6:1, SIMPLIFY = FALSE))

mapply(word, "A", integer()) # gave Error, now list()


[Package base version 4.2.0 Index]