Suppose I wish to relevel the Species in iris
dataset so that the reference level becomes "virginica"
want_iris <- iris %>%
mutate(Species = relevel(factor(Species), ref = "virginica"))
want_iris$Species
...
[141] virginica virginica virginica virginica virginica virginica virginica virginica virginica virginica
Levels: virginica setosa versicolor
However, let's say I wish to dynamically change the variable (Species) and reference level (virginica)
var_name <- "Species"
ref_name <- "virginica"
test_iris <- iris %>%
mutate({{var_name}} := relevel(factor({{var_name}}), ref = {{ref_name}}))
test_iris$Species
Error: Problem with `mutate()` column `Species`.
i `Species = relevel(factor("Species"), ref = "virginica")`.
x 'ref' must be an existing level
From what I gather from these posts (1, 2), using dynamic variable on the right side of the dplyr is not straight forward, and I actually asked a similar question on 3, although the question was limited to the column name.
My rough guess is that since {{}}
unquotes the variable name, I am specifying ref = virginica
instead of ref = "virginica"
How may I approach this problem?