let Example

Nina Zumel

2019-07-24

An example of using let to wrap dplyr expressions as functions.

Note: let has been moved to the wrapr package.

library("dplyr")
library("replyr")

The desired task: write a function that takes a data frame with a specified numerical column and an optional grouping column, and returns a data frame with one row per group containing:

The dplyr expression for such a table is easy when the column names are known, but complicated when they are not. We use wrapr::let to write such a function without the use of lazyeval or rlang/tidyeval.

sumstat_intervals = function(dframe, colname, groupcolname = NULL) {
  mapping = list(COLNAME = colname,
                 GROUPCOLNAME = groupcolname)
  let(alias = mapping,
      {
        if(!is.null(groupcolname)) {
          dframe <- group_by(dframe, GROUPCOLNAME)
        }
        summarize(dframe, 
                  sdlower = mean(COLNAME)-sd(COLNAME),
                  mean = mean(COLNAME),
                  sdupper = mean(COLNAME) + sd(COLNAME),
                  iqrlower = median(COLNAME)-0.5*IQR(COLNAME),
                  median = median(COLNAME),
                  iqrupper = median(COLNAME)+0.5*IQR(COLNAME))
      })
}

We can test sumstat_intervals on iris:

sumstat_intervals(iris, "Sepal.Length")
 #     sdlower     mean  sdupper iqrlower median iqrupper
 #  1 5.015267 5.843333 6.671399     5.15    5.8     6.45
sumstat_intervals(iris, "Sepal.Length", "Species")
 #  # A tibble: 3 x 7
 #    Species    sdlower  mean sdupper iqrlower median iqrupper
 #    <fct>        <dbl> <dbl>   <dbl>    <dbl>  <dbl>    <dbl>
 #  1 setosa        4.65  5.01    5.36     4.8     5       5.2 
 #  2 versicolor    5.42  5.94    6.45     5.55    5.9     6.25
 #  3 virginica     5.95  6.59    7.22     6.16    6.5     6.84
sumstat_intervals(iris, "Petal.Length", "Species")
 #  # A tibble: 3 x 7
 #    Species    sdlower  mean sdupper iqrlower median iqrupper
 #    <fct>        <dbl> <dbl>   <dbl>    <dbl>  <dbl>    <dbl>
 #  1 setosa        1.29  1.46    1.64     1.41   1.5      1.59
 #  2 versicolor    3.79  4.26    4.73     4.05   4.35     4.65
 #  3 virginica     5.00  5.55    6.10     5.16   5.55     5.94