7.3 Apply a function over multiple lists with mapply
7: Data Munging
7.3 Apply a function over multiple lists with mapply - Video Tutorials & Practice Problems
Video duration:
4m
Play a video:
<v Voiceover>There will be times</v> when you have two or more lists and you want to iterate over them together at the same time. Most new R programmers and even seasoned R programmers say, "well, how am I going to do this without a loop?" They build a fore loop and iterate through it. Fortunately that is not necessary, because of the mapply function. To illustrate this, let's go ahead and build two lists. The firstList will get, A will be a matrix and we'll say one through 16 and make it four rows. B will also be a matrix. This will be one through 16. It'll have two rows and C will be the vector one through five. We'll make secondList, and we'll make A also a matrix of the numbers one through 16, and four will be the number of rows. B will be a matrix, one through 16, but this time we'll give it eight rows. C will be 15 through one. We made a little error in here, and that's because every argument inside list has to be separated by a comma. It's also important to close off your matrix function when including it inside another statement. Now notice here, in the console, R is running, and this little plus sign means it's waiting for further instructions, because it didn't see a closing parentheses here. It assumed this parenthesis was used to close the matrix, and it's still waiting for either more arguments or closing parenthesis to list. Since we actually made a mistake, we don't want that, we will hit escape and rerun this line. We could look at our lists by saying "firstList" and "secondList." We scroll through this, we see firstList as a nice A matrix, a B matrix, and a vector, and the secondList has the same A matrix, a different B matrix, and a different C vector. Now that we have these built, we want to go through element by element and see, are they identical? Is A in firstList identical to A in secondList? Is B in firstList identical to B in secondList? We'll use mapply, and unlike lapply and apply where the first argument, was the object you're iterating over, for mapply, the first argument is the function you're going to be applying. In this case, that is identical. The subsequent arguments are lists you're iterating over, in this case, firstList and secondList. We run this, and we get back a vector in this case of trues and falses, because, if you recall, A is the exact same thing, a one through 16 matrix of four rows, whereas B, they're both one through 16, one is two rows, one is eight rows, and C are just completely different vectors. That is mapply going over multiple lists. For all these functions, the whole apply family, we can supply a user written function. Let's build a function that adds the rows or lengths of each element in one list to the rows or lengths of each element in the other list. Before we do that, let's clear the screen to make some room. We will say simplefunc gets function, X and Y as arguments. Then we simply return NROW X plus NROW Y. Now notice, we want to get you to the rows or the lengths. When we feed a matrix to capital NROW, it'll give us the number of rows. When we feed a vector to capital NROW, it will give us the length of that vector. That's why this NROW function is a nice catch-all. Let's run this function so that way we can use it. Let's apply that to both of our lists. Let's do mapply. It's first argument is this function. And the subsequent arguments are the lists we want to go over. So it's firstList, secondList, and we see that. It took the number of rows in matrix A in the firstList, added to the number of rows of matrix A in the secondList. Same thing for element B, same thing for element C. Mapply is a great way to iterate over multiple lists and it really voids that trap of trying to fall into a loop.