This post aims to explore some basic concepts of do(), along with giving some advice in using and programming.

do() is a verb (function) of dplyr. dplyr is a powerful R package for data manipulation, written and maintained by Hadley Wickham. This package allows you to perform the common data manipulation tasks on data frames, like: filtering for rows, selecting specific columns, re-ordering rows, adding new columns, summarizing data and computing arbitrary operations.

First of all, you have to install dplyr package:

and to load it:

We will analyze the use of do() with the following dataset, created with random data:

We firstly transform it into a tbl_df object to achieve a better print method. No changes occur on the input data frame.

Base Concepts of do() (Non Standard Evaluation Version)

As we already said, do() computes arbitrary operations on a data frame returning more than one number back.

To use do(), you must know that:

  • it always returns a dataframe
  • unlike the others data manipulation verbs of dplyr, do() needs the specification of . placeholder inside the function to apply, referring to the data it has to work with.
  • it is conceived to be used with dplyr group_by() to compute operations within groups:
  • the argument of do() can be named or unnamed:
    • named arguments (more than one supplied) become list-columns, with one element for each group:
    • unnamed argument (only one supplied) must be a data frame and labels will be duplicated accordingly:

Its use is the same working with customized functions.

Let us define the following function, which performs two simple operations returning a data frame:

If the argument is named the result is:

Otherwise, if argument is unnamed the result is:

Programming with do_() (Standard Evaluation Version)

How can we enclose the previous operations inside a function? Simple! Using do_() (the SE version of do()) and interp() function of lazyeval package.

lazyeval is an R package, written and maintained by Hadley Wickham. It represents a new approach to Non Standard Evaluation (NSE) for R. The difference between SE and NSE approaches is the quoting of input variable names. NSE is suitable for interactive use (see the previous paragraph), but not for programming, for which SE approach is recommended.

Install and load lazyeval, if you haven’t already done it.

interp() helps to build the expression up from a mixture of constants and variables to be passed to .dots argument of dplyr verbs. For more details see Non Standard Evaluation vignette.

In the following example interp() is used to build up the expression to be passed to .dots argument of group_by_() (SE version of group_by()), which consists of the grouping variable name. It is used also to build up the expression to be passed to .dots argument of do_(). This expression consists of the function name specifying also its arguments in brackets.

Let us apply the previous function to ds dataset:

Other Examples

do() is often used to fit models and to display the results.
Look at the following functions!

Let us define a function that fits linear model and returns coefficients as a data frame and apply it to ds by group:

Let us enclose the previous operations inside a function and apply it to ds by group: