rdplyr

How to use a variable in dplyr::filter?


I have a variable with the same name as a column in a dataframe:

df <- data.frame(a=c(1,2,3), b=c(4,5,6))
b <- 5

I want to get the rows where df$b == b, but dplyr interprets this as df$b == df$b:

df %>% filter(b == b) # interpreted as df$b == df$b
#   a b
# 1 1 4
# 2 2 5
# 3 3 6

If I change the variable name, it works:

B <- 5
df %>% filter(b == B) # interpreted as df$b == B
#   a b
# 1 2 5

I'm wondering if there is a better way to tell filter that b refers to an outside variable.


Solution

  • You could use the get function to fetch the value of the variable from the environment.

    df %>% filter(b == get("b")) # Note the "" around b