0:05

mapply is a loop function that tries, is a multivariate version

Â of the kind of lapply and sapply functions that you've seen previously.

Â And the idea is that it applies a

Â function in parallel over a set of different arguments.

Â So one thing we have noticed about

Â the previous functions, lapply, sapply, tapply, is that,

Â they have only, they only apply a function over the elements of a single object.

Â So, for example, if you think about lapply,

Â the input to lapply was a list. A single list, that is.

Â And then the function was applied over the elements of that list.

Â So, what happens if you have two lists that you want to apply a function over?

Â And so, and suppose you have two lists, and

Â the elements of the first list go into one

Â argument of the function, and the elements of the

Â second list go into another argument of the function.

Â So lapply and sapply can't really be used for that purpose.

Â So one way to do that is just to write

Â a for loop, where the for loop will index the elements

Â of each of the different lists, and then you can

Â pass a function to each of those elements of the list.

Â Another way to do that though is with

Â mapply, where mapply can take multiple list arguments and

Â then apply a function to the, to the

Â elements of those, of the multiple lists, in parallel.

Â So the function arguments for mapply are a little bit different, just because it has

Â to allow for the possibility of a variable number of arguments.

Â So here, the first argument to mapply is the function that you want to apply.

Â And the function that you're going to pass to mapply

Â has to have, the number of arguments that the function

Â takes has to be at least as many as the

Â number of lists that you're going to pass to mapply.

Â So the list that, the things that will be coerced to

Â a list, will be passed through the dot dot dot argument.

Â And so if you have three lists, you'll pass three objects and

Â then your function has to take at least three arguments to it.

Â So, the more args, argument is just if you have

Â more arguments and you need to pass to your function.

Â And a simplified argument is similar to the simplify arguments

Â that you saw in sapply and also in in tapply.

Â 2:07

So, here, for example, I'm creating a list here, and

Â the list has, I'm going to repeat 1 four times, the

Â integer 1 four times, I'm going to repeat 2 three times,

Â I'm going to repeat 3 two times, and repeat 4 just once.

Â So it's a little bit tedious to have to type something to do, even

Â though this is a fairly artificial example,

Â but with mapply, it's actually quite simple.

Â I can just do mapply rep, so, rep is the repeat

Â function And then, repeat, it has two arguments, so, the first set

Â of arguments is going to be 1 through 4, and the second set of arguments is

Â going to be 4 through 1, and you can see that in list above here.

Â The first argument was 1, 2, 3, and 4, and the second argument was 4, 3, 2, and 1.

Â So, that's, so those are the two sets

Â of arguments that I'm going to pass to mapply.

Â And you can see that when I do that, I get my list of four 1s,

Â three 2s, two 3s and one 4, just like the list that I have above here.

Â So mapply is, can be used to apply a function to multiple sets of arguments.

Â 3:07

So, here's just another very simple function.

Â It just generates some random normal noise.

Â And these, and, see, the rnorm, the, the,

Â I'm sorry, the function has three arguments the,

Â the number of observations the value of the

Â mean, and the value of the standard deviation.

Â So, if I just apply noise to, with a single set of arguments, 5, 1 and 2.

Â I get 5 random normal variables with the mean 1 and standard variation 2.

Â However, this function doesn't work

Â really correctly if I pass it a vector of arguments.

Â So, now what's happening is, I get a vector of 5, here.

Â When I pass it 1 through 5 and 1 through 5.

Â But, really what I want to happen is to

Â have one, one random normal with mean 1, two random

Â normals with mean 2, three random normals with mean

Â 3, etc, and then five random normals with mean 5.

Â And, so that's what I get here, when I use

Â the mapply function onto the and if I vectorize this noise function I give it,

Â you know, three sets of arguments, so it's 1 through 5, 1 through 5, and then 2.

Â So I, I'm always going to fix the standard deviation to be 2, but

Â I'm going to be changing the n and I'm going to be changing the mean.

Â So now I've got one random variable with a mean 1, I got two with

Â mean 2, three with mean 3, four with mean 4 and then five with mean 5.

Â So that's how I can instantly

Â vectorize a function that doesn't allow for vector arguments.

Â