Hands-on dplyr tutorial for faster data manipulation in R

September 23, 2014

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

I love dplyr. It’s my “go-to” package in R for data exploration, data manipulation, and feature engineering. I use dplyr because it saves me time: its performance is blazing fast on data frames, but even more importantly, I can write dplyr code faster than base R code. Its syntax is intuitive and its functions are well-named, and so dplyr code is easy-to-read even if you didn’t write it.

dplyr is the “next iteration” of the plyr package (focusing data frames, hence the “d”), and released version 0.1 in January 2014. It’s being developed by Hadley Wickham (author of plyr, ggplot, devtools, stringr, and many other R packages), so you know it’s a well-written, well-documented package.

Teaching dplyr using an R Markdown document

As one of the instructors for General Assembly’s 11-week Data Science course in Washington, DC, I had 30 minutes in class last week to talk about data manipulation in R, and chose to focus exclusively on dplyr. When putting together my presentation, I had a lot of great material to draw from:

I decided to create an R Markdown document to present from, since that would allow me to blend together R code, output, and explanatory text. You can view the final document on RPubs. (Update: It has also been translated into Indonesian!)

Using the hflights dataset (available on CRAN), I demonstrate the five basic dplyr “verbs,” the chaining syntax, some of the more advanced functionality (such as window functions), a few of the new convenience functions that I find most useful (such as glimpse and summarise_each), and how to query a database using dplyr. I also compare many of the dplyr commands to the equivalent commands in base R. (Thanks to Hadley, because many of the examples I use are ones he wrote!)

Watch the dplyr tutorial on YouTube

After presenting, I recorded the entire presentation as a YouTube video (embedded below), since I know it can be helpful to hear someone explaining code that is unfamiliar to you. It runs 39 minutes, but if you only want to watch a particular section, simply click the topic below and it will skip to that point in the video.

  1. Introduction to dplyr (starts at 0:00)
  2. Loading dplyr and the example dataset (starts at 2:29)
  3. Understanding “local data frames” (starts at 3:23)
  4. Verb #1: filter (starts at 5:17)
  5. Verb #2: select, plus contains, starts_with, ends_with, matches (starts at 7:54)
  6. Using chaining syntax for more readable code (starts at 9:34)
  7. Verb #3: arrange (starts at 12:53)
  8. Verb #4: mutate (starts at 13:55)
  9. Verb #5: summarise, plus group_by, summarise_each, n, n_distinct, tally (starts at 15:31)
  10. Window functions: min_rank, top_n, lag (starts at 26:47)
  11. Convenience functions: sample_n, sample_frac, glimpse (starts at 32:44)
  12. Connecting to databases (starts at 34:21)

What topics did I miss? What do you like most about dplyr? Let me know in the comments!

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

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.


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)