Description Usage Arguments Details Value Examples
Take the a string value and use it as a column name in a dplyr function.
1 | treat_string_as_col(arg)
|
arg |
the argument that holds a value to be used as a column name. |
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.
something that will resolve to a column name when prefixed with '!!'.
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.