Back to home

Contents

Description
Dplyr - indirect references

Description

The objective of this document is to be a reference for topics about the R programming language itself, not so much about statistics or data manipulation.

It’s a work in progress and the first example below is good to demonstrate the kind of matter that will be treated here.

Dplyr - indirect references

The code below demonstrates how to make an indirect reference to ‘key = value’ arguments in dplyr functions:

suppressPackageStartupMessages(library(dplyr))

cols = names(iris)[2:4]  # select remaining columns 
col_syms = syms(cols)  # create symbols from strings

summary_vars <- lapply(col_syms, function(col) {
  expr(last(!!col))  # expression that should be evaluated in summarise
})
names(summary_vars) = cols  # new column names (set old names)

iris %>%  
  group_by(Species) %>%
  summarise(Sepal.Length = mean(Sepal.Length), !!!summary_vars)  # open expressions
## `summarise()` ungrouping output (override with `.groups` argument)
## # A tibble: 3 x 5
##   Species    Sepal.Length Sepal.Width Petal.Length Petal.Width
##   <fct>             <dbl>       <dbl>        <dbl>       <dbl>
## 1 setosa             5.01         3.3          1.4         0.2
## 2 versicolor         5.94         2.8          4.1         1.3
## 3 virginica          6.59         3            5.1         1.8