Reputation: 12677
I'd like to define a function that takes a data.frame and a column name and returns the data.frame in with that column transformed (e.g. to lowercase). When the column name is known in advance, this is simple:
diamonds %>% mutate(cut = tolower(cut))
How do I define a function foo
, such that:
col <- "cut"
foo(diamonds, col)
does this same behavior? (Not looking for a base R or data.table
answer since I want to preserve dplyr
's ability to translate this into a lazily-evaluated SQL call).
If I just wanted my function to work using: foo(diamonds, cut)
, I just need enquo
and !!
foo <- function(df, col){
x <- enquo(col)
mutate(df, !!x := tolower(!!x))
}
If I want to take the column name in quotes, foo(diamonds, "cut")
, adding ensym
is sufficient:
foo <- function(df, col){
col <- ensym(col)
x <- enquo(col)
mutate(df, !!x := tolower(!!x))
}
but this fails when given a variable for an argument:
col <- "cut"
foo(diamonds, col)
Error in ~col : object 'col' not found
What am I missing that can evaluate the variable?
Upvotes: 2
Views: 1038
Reputation: 4767
Going back to your original example, just use ensym()
to convert text arguments to symbols, there is no need for a quosure in this case.
library(ggplot2)
col <- "cut"
foo <- function(df, col){
col <- rlang::sym(col)
dplyr::mutate(df, !!col := tolower(!!col))
}
foo(diamonds, col)
#> # A tibble: 53,940 x 10
#> carat cut color clarity depth table price x y z
#> <dbl> <chr> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
#> 1 0.23 ideal E SI2 61.5 55 326 3.95 3.98 2.43
#> 2 0.21 premium E SI1 59.8 61 326 3.89 3.84 2.31
#> 3 0.23 good E VS1 56.9 65 327 4.05 4.07 2.31
#> 4 0.290 premium I VS2 62.4 58 334 4.2 4.23 2.63
#> 5 0.31 good J SI2 63.3 58 335 4.34 4.35 2.75
#> 6 0.24 very good J VVS2 62.8 57 336 3.94 3.96 2.48
#> 7 0.24 very good I VVS1 62.3 57 336 3.95 3.98 2.47
#> 8 0.26 very good H SI1 61.9 55 337 4.07 4.11 2.53
#> 9 0.22 fair E VS2 65.1 61 337 3.87 3.78 2.49
#> 10 0.23 very good H VS1 59.4 61 338 4 4.05 2.39
#> # … with 53,930 more rows
Created on 2019-03-11 by the reprex package (v0.2.1)
Upvotes: 2
Reputation: 2601
You can also avoid tidy eval entirely here by using mutate_at()
.
library(tidyverse)
(x <- tibble(
num = 1:3,
month = month.abb[num]
))
#> # A tibble: 3 x 2
#> num month
#> <int> <chr>
#> 1 1 Jan
#> 2 2 Feb
#> 3 3 Mar
x %>%
mutate(month = tolower(month))
#> # A tibble: 3 x 2
#> num month
#> <int> <chr>
#> 1 1 jan
#> 2 2 feb
#> 3 3 mar
foo <- function(df, col) {
mutate_at(df, .vars = col, .funs = tolower)
}
foo(x, "month")
#> # A tibble: 3 x 2
#> num month
#> <int> <chr>
#> 1 1 jan
#> 2 2 feb
#> 3 3 mar
this <- "month"
foo(x, this)
#> # A tibble: 3 x 2
#> num month
#> <int> <chr>
#> 1 1 jan
#> 2 2 feb
#> 3 3 mar
Created on 2019-03-09 by the reprex package (v0.2.1.9000)
Upvotes: 7
Reputation: 595
library(tidyverse)
col <- "cut"
foo <- function(df, col) {
df %>%
mutate(!!sym(col) := tolower(!!sym(col)))
}
foo(diamonds, col)
Check out Pass a string as variable name in dplyr::filter.
Upvotes: 5