treat_string_as_col: treat_string_as_col

Description Usage Arguments Details Value Examples

Description

Take the a string value and use it as a column name in a dplyr function.

Usage

1

Arguments

arg

the argument that holds a value to be used as a column name.

Details

This is used to take the string value of a variable and use it in place of a literal column name when calling a dplyr function. This ability is useful when the name of the column to operate on is determined at run-time from data.

Value

something that will resolve to a column name when prefixed with '!!'.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
## Not run: 
## drop this run-time determined column.
b <- "cyl"
 select(mtcars, -!!treat_string_as_col(b))

## function double a column
double_col <- function(dat, arg) {
  dplyr::mutate(dat, result = !!rlang::sym(arg) * 2)
}
double_col(mtcars, arg = 'cyl')


## End(Not run)

MilesMcBain/rlung documentation built on May 21, 2019, 9:22 a.m.