# Reading: The Split-Apply-Combine Strategy for Data Analysis

Toggle Code Viewing: ON

As part of the course Stat 585X I am taking this semester, I will be posting a series of responses to assigned course readings. Mostly these will be my rambling thoughts as I skim papers.

This week we are reading “The Split-Apply-Combine Strategy for Data Analysis” by Hadley Wickham, published in the Journal of Statistical Software, April 2011, Volume 40, Issue 1. This paper details the split-apply-combine strategy in data analysis and its implementation in R in a package called plyr.

### Split-Apply-Combine

I posit that the split-apply-combine strategy is useful in a much larger realm than data analysis. I liken it to the idea of ‘divide and conquer’. The idea is to split up a large task into smaller, bite-sized chunks, apply the solution on each piece independently, and then combine these little pieces back into the final result. Of course, we can accomplish this with base R functions, but plyr provides a very intuitive command structure and naming consistency, not to mention it lessens the number of lines of code I have to write.

So what does it truly mean to split-combine-apply? What are we splitting? The easiest way I can illustrate the idea is with the concept of a data frame. Imagine we have a data frame with three columns: an identity, a group, and a metric. We may want to know how many identities there are in each group, or maybe we want to know the sum of the metric values within each group. The split-apply-combine strategy will allow us to do this in the following manner:

It’s easiest to conceptualize plyr-ing a data frame because it’s two dimensional. Of course, we can apply the split-apply-combine strategy to objects of more than one dimension. Hadley explains this idea very thoroughly with great images in the paper and I recommend the read if you are using more complicated data structures.

### From what to what?

As I mentioned, one of my favorite things about the plyr package is its intuitive naming structure. The way the functions within the package are named defines what data structure is being split [input] and then what format to combine the results into [output]. More specificaly, the functions used to split-apply-combine are names as [input letter][output letter]ply. There are three structures you can use as either input or output.

1. Arrays – letter: a
2. Data frames – letter: d
3. Lists – letter: l

The type of input determines how the opject can be split and what kind of functions can be applied. For example, ddply takes a data frame as input and returns a data frame as output. This naming convention reduced confusion and time spent re-looking up helpfiles when I try to plyr.

### Tips Example

To show off the ease of the plyr solutions, I’d like to walk through a small example using the tips dataset from the reshape2 package. This is a dataset with recorded information about each tip he received over a period of a few months working in one restaurant.

Let’s say we want to look at average tips earned on Fridays for the server. We could employ a subset strategy, like so:

But what about average tips for each day? I wouldn’t want to have to do each day by hand. Instead, let’s use plyr!

In the function call you can see we are specifying day to be the group variable and then we want to ‘summarise’ our dataset my displaying the mean tip for each day. We can also easily break this out by meal type.

From here it’s easy to see that larger tips happen at dinner and the server actually received his largest tips at dinner.

This is a small example of what we can do easily with plyr. But there is a vast expanse of things I haven’t even touched on.

### Something new

I have been using plyr for over a year now, but I learned something completely new from reading Hadley’s paper. I didn’t know that you can use formulas and renaming in the variable call to plyr. For example, say we wanted to group by “time_day” and call it “time” for whatever reason. This is possible with the same simple syntax.

I know this is a completely contrived example, but I think this little nugget of knowledge could be really useful to me in the future.

### Commentary

I hope you get some good mileage out of the split-apply-combine strategy. I use it all the time and really enjoyed reading this paper to learn more deeply about the ideas underlying the plyr package. Let me know in the comments section how plyr is working for you!