When I use plyr/dplyr

January 24, 2014

(This article was first published on Educate-R - R, and kindly contributed to R-bloggers)

My last post I talked about how I use the data.table package for aggregating and removing duplicate observations. Although I use the data.table package quite often, there are many times when I use plyr (and now the new dplyr) package, primarily because of its easy, intuitive syntax.


One of my personal favorite functions in the plyr suite of basic functions is the arrange function. The base functions for sorting/ordering are more difficult to use. Not to mention there have been many times that I have used the base::sort function when I really need to use the base::order function (sort to me is the word I think of first). arrange is great due to the easy, general syntax used for it as shown below:

arrange(dataframe, col1, col2, col3)

When using the base::order function, this needs to be done through the indexing operators and is not nearly as intuitive to me. I always have to think for a second to get it right. Here are two general examples:

dataframe[order(dataframe$col1, dataframe$col2, dataframe$col3), ]
with(dataframe, dataframe[order(col1, col2, col3), ])

Both involve much more typing and are more difficult to read the code in my opinion.

Simple, Intuitive syntax

The other aspect of the plyr (and dplyr) suite of functions that keeps me coming back is their simple, intuitive syntax. For example, if I am teaching a student how to aggregate or sort, plyr is my go to package. Easy to explain, easy to understand. The common structure across all of the functions is brilliantly programmed and a standard for everyone else to replicate.

New! Bonus use for dplyr

The new ability to use the chain function or alternatively the %.% operator is a great addition to R. One of the difficulties with code readability in R is the whenever functions are nested together. By default R interprets from inside to out, not how most of us read written words let alone code. The chain function and %.% operator allows the user to write the functions in the order they will be processed by R, therefore the code can read from left to right.

Using the mtcars dataset, suppose we wanted to select specific columns, aggregate the miles per gallon and weight by the number of cylinders and automatic transmission status, and filter so we select the rows that have an average miles per gallon greater than 20.

mtcars %.% 
  group_by(cyl, am) %.%
  select(mpg, cyl, wt, am) %.%
  summarise(avgmpg = mean(mpg), avgwt = mean(wt)) %.%
  filter(avgmpg > 20)


## Source: local data frame [3 x 4]
## Groups: cyl
##   cyl am avgmpg avgwt
## 1   4  0  22.90 2.935
## 2   4  1  28.07 2.042
## 3   6  1  20.57 2.755

Compare the above syntax to:

      group_by(mtcars, cyl, am),
      mpg, cyl, wt, am),
    avgmpg = mean(mpg), avgwt = mean(wt)),
  avgmpg > 20)


## Source: local data frame [3 x 4]
## Groups: cyl
##   cyl am avgmpg avgwt
## 1   4  0  22.90 2.935
## 2   4  1  28.07 2.042
## 3   6  1  20.57 2.755

Both chunks give you the same result, however the first one is much easier to see the process taken to get to the end result. Much easier to adapt the code to add/remove parts of it.


I use both data.table and plyr/dplyr packages. All of these packages are a great tool for certian data problems. If I want to write a single line of code to do a lot of manipulations I will tend to use data.table. However, if I am writing code where I am doing more exploration or if I am collaborating with others I tend to write my code using plyr/dplyr. The versatility that both packages bring together in tandem is an excellent and powerful combination. I do not have time to be a complete package elitest, the correct tool for the right data problem is the best solution for me.

To leave a comment for the author, please follow the link and comment on their blog: Educate-R - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)