Harry M
Harry M

Reputation: 2088

Write a tidyeval function to rename a factor level in a dplyr

I'm trying to write a tidyeval function that takes a numeric column, replaces values above a certain limit with the value for limit, turns that column into a factor and then replaces the factor level equal to limit with a level called "limit+".

For example, I'm trying to replace any value above 3 in sepal.width with 3 and then rename that factor level to 3+.

As an example, here's how I'm trying to make it work with the iris dataset. The fct_recode() function is not renaming the factor level properly, though.

plot_hist <- function(x, col, limit) {
  col_enq <- enquo(col)
  x %>% 
    mutate(var = factor(ifelse(!!col_enq > limit, limit,!!col_enq)),
           var = fct_recode(var, assign(paste(limit,"+", sep = ""), paste(limit))))
}

plot_hist(iris, Sepal.Width, 3)

Upvotes: 2

Views: 532

Answers (1)

Thomas K
Thomas K

Reputation: 3311

To fix the last line, we can use the special symbol :=, since we need to set the value at the left hand side of the expression. For the RHS we need to coerce to character, since fct_recode expects a character vector on the right.

library(tidyverse)


plot_hist <- function(x, col, limit) {
  col_enq <- enquo(col)

  x %>% 
    mutate(var = factor(ifelse(!!col_enq > limit, limit, !!col_enq)),
           var = fct_recode(var, !!paste0(limit, "+") := as.character(limit)))
}

plot_hist(iris, Sepal.Width, 3) %>% 
  sample_n(10)
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species var
#> 40           5.1         3.4          1.5         0.2     setosa  3+
#> 98           6.2         2.9          4.3         1.3 versicolor 2.9
#> 7            4.6         3.4          1.4         0.3     setosa  3+
#> 99           5.1         2.5          3.0         1.1 versicolor 2.5
#> 76           6.6         3.0          4.4         1.4 versicolor  3+
#> 77           6.8         2.8          4.8         1.4 versicolor 2.8
#> 85           5.4         3.0          4.5         1.5 versicolor  3+
#> 119          7.7         2.6          6.9         2.3  virginica 2.6
#> 110          7.2         3.6          6.1         2.5  virginica  3+
#> 103          7.1         3.0          5.9         2.1  virginica  3+

Upvotes: 5

Related Questions