Nested loops with mapply

So as I sink deeper into the second level of R enlightenment, one thing troubled me. “lapply” is fine for looping over a single vector of elements, but it doesn’t do a nested loop structure. These tend to be pretty ubiquitous for me. I’m forever doing the same thing to a set of two or three different variables. “apply ” smells like a logical candidate, but it will really only allow to you to do the same operation over a set of vectors. Meh. “tapply” is more of the same, but applies over a “ragged” array. But “mapply” fits the bill. As it turns out, using mapply is incredibly easy. I found that the trickiest thing to implement is the logic to create a set of all possible combinations over which I want to loop.

Let’s look at that first. Say that you have three variables. To keep things simple, each one is a two-dimensional character vector as below.

a = c("A", "B")
b = c("L", "M")
c = c("X", "Y")

I poked around for a function that would easily render the Cartesian product of those three vectors. Interaction seemed like a natural choice, but it seems as though it wants to work with factors and my first attempts to use it returned an error which had something to do with the number of elements. Diagnosing errors in R can be a Kafka-esque adventure and you have to choose your battles. I decided to look elsewhere. An easy way to do that is to handle it manually if you only have two vectors. Just replicate each, order one of them and bind the results together, sort of like this:

The ordering step is necessary so that all combinations are represented. So, this is fine for two variables, but won’t work for three or more. Extension of the idea above is straightforward. After two variables, you have a matrix and you simply need to replicate it, just as you would a vector. I coded a function that would take two arguments. The first is a matrix (or a vector) and the second is the next vector we want to reflect.

Note that using rep or replicate with a character matrix may not give you the results you intended. rep converts a matrix into a one-dimensional array. So, I coerce results into matrices and replicate using a list structure, rather than the simplified result from replicate.

Although I find that a novel use of sapply, I don’t think it would work in this case. The nesting of sapply which you illustrate presumes that the output of a function may also serve as the input. The specific problem which I had was rapid application of a function which pulls NFL results for a single season for a single team. Given a vector of seasons and a vector of teams, I wanted to get a dataframe which had results for all teams and all seasons. Having done that, I thought about how to apply it generally to problems where I was simply running the same function against combinations of many variables.

One of the trickiest things about R is that it’s so hard to identify solutions that you presume must be out there. I had done some searching on the word “cartesian” but only turned up some ggplot2 functions. No way in the world would I have guessed that expand.grid was what I was looking for. Great tip!