I have a data frame and want to filter it in one of two ways, by either column "this" or column "that". I would like to be able to refer to the column name as a variable. How (in dplyr
, if that makes a difference) do I refer to a column name by a variable?
library(dplyr)
df <- data.frame(this = c(1, 2, 2), that = c(1, 1, 2))
df
# this that
# 1 1 1
# 2 2 1
# 3 2 2
df %>% filter(this == 1)
# this that
# 1 1 1
But say I want to use the variable column
to hold either "this" or "that", and filter on whatever the value of column
is. Both as.symbol
and get
work in other contexts, but not this:
column <- "this"
df %>% filter(as.symbol(column) == 1)
# [1] this that
# <0 rows> (or 0-length row.names)
df %>% filter(get(column) == 1)
# Error in get("this") : object 'this' not found
How can I turn the value of column
into a column name?
Best Answer
From the current dplyr help file (emphasis by me):
So we basically need to do two things, to be able to refer to the value
"this"
of the variablecolumn
insidedplyr::filter()
:We need to turn the variable
column
which is of type character into typesymbol
.Using base R this can be achieved by the function
as.symbol()
which is an alias foras.name()
. The former is preferred by the tidyverse developers because itAlternatively the same can be achieved by
rlang::sym()
from the tidyverse.We need to unquote the symbol from 1).
What unquoting exactly means can be learned in the vignette Programming with dplyr. It is achieved by the syntactic sugar
!!
.(In earlier versions of
dplyr
(or the underlyingrlang
respectively) there used to be situations (incl. yours) where!!
would collide with the single!
, but this is not an issue anymore since!!
gained the right operator precedence.)Applied to your example: