0% found this document useful (0 votes)
17 views

The R Language

mapply applies a function to multiple arguments in parallel and returns the results in a list. It takes a function FUN as the first argument, followed by any number of arguments to apply FUN over. MoreArgs allows additional arguments to pass to FUN. It simplifies the results by default. mapply can instantly vectorize a function like noise, which generates random numbers, so it applies the noise function to each set of arguments in 1:5 rather than calling it sequentially. This allows generating multiple sets of random numbers with one call to mapply rather than writing out the equivalent list call.

Uploaded by

juntujuntu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

The R Language

mapply applies a function to multiple arguments in parallel and returns the results in a list. It takes a function FUN as the first argument, followed by any number of arguments to apply FUN over. MoreArgs allows additional arguments to pass to FUN. It simplifies the results by default. mapply can instantly vectorize a function like noise, which generates random numbers, so it applies the noise function to each set of arguments in 1:5 rather than calling it sequentially. This allows generating multiple sets of random numbers with one call to mapply rather than writing out the equivalent list call.

Uploaded by

juntujuntu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

mapply

mapply is a multivariate apply of sorts which applies a function in parallel over a set of
arguments.
> str(mapply)
function (FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE,
USE.NAMES = TRUE)
FUN is a function to apply
... contains arguments to apply over
MoreArgs is a list of other arguments to FUN.
SIMPLIFY indicates whether the result should be simplified

The R Language

mapply
The following is tedious to type
list(rep(1, 4), rep(2, 3), rep(3, 2), rep(4, 1))
Instead we can do
> mapply(rep, 1:4, 4:1)
[[1]]
[1] 1 1 1 1
[[2]]
[1] 2 2 2
[[3]]
[1] 3 3
[[4]]
[1] 4
The R Language

Vectorizing a Function

> noise <- function(n, mean, sd) {


+
rnorm(n, mean, sd)
+ }
> noise(5, 1, 2)
[1] 2.4831198 2.4790100 0.4855190 -1.2117759
[5] -0.2743532
> noise(1:5, 1:5, 2)
[1] -4.2128648 -0.3989266
[5] 3.7413584

4.2507057

1.1572738

The R Language

Instant Vectorization
> mapply(noise, 1:5, 1:5, 2)
[[1]]
[1] 1.037658
[[2]]
[1] 0.7113482 2.7555797
[[3]]
[1] 2.769527 1.643568 4.597882
[[4]]
[1] 4.476741 5.658653 3.962813 1.204284
[[5]]
[1] 4.797123 6.314616 4.969892 6.530432 6.723254
The R Language

Instant Vectorization

Which is the same as


list(noise(1, 1, 2), noise(2, 2, 2),
noise(3, 3, 2), noise(4, 4, 2),
noise(5, 5, 2))

The R Language

You might also like