I want to use variable names as strings in functions of dplyr
. See the example below:
df <- data.frame(
color = c("blue", "black", "blue", "blue", "black"),
value = 1:5)
filter(df, color == "blue")
It works perfectly, but I would like to refer to color
by string, something like this:
var <- "color"
filter(df, this_probably_should_be_a_function(var) == "blue").
I would be happy, to do this by any means and super-happy to make use of easy-to-read dplyr
syntax.
In the newer versions, we can use we can create the variables as quoted and then unquote (UQ
or !!
) for evaluation
var <- quo(color)
filter(df, UQ(var) == "blue")
# color value
#1 blue 1
#2 blue 3
#3 blue 4
Due to operator precedence, we may require ()
to wrap around !!
filter(df, (!!var) == "blue")
# color value
#1 blue 1
#2 blue 3
#3 blue 4
With new version, ||
have higher precedence, so
filter(df, !! var == "blue")
should work (as @Moody_Mudskipper commented)
We may also use:
filter(df, get(var, envir=as.environment(df))=="blue")
#color value
#1 blue 1
#2 blue 3
#3 blue 4
EDIT: Rearranged the order of solutions