I need to filter the table by a logical column (more precisely, by negating it), but the column name may be different. It is easy when I know their names in advance:
tb = tibble(
id = 1:4,
col1 = c(TRUE, TRUE, FALSE, FALSE),
col2 = c(TRUE, FALSE, TRUE, FALSE)
)
tb
#
#
#
#
#
#
#
colname = quo(col1)
tb %>%
filter(!!colname) # rows where col1 is true
#
#
#
#
#
tb %>%
filter(!(!!colname)) # rows where col1 is false
#
#
#
#
#
colname = quo(col2)
tb %>%
filter(!!colname) # rows where col2 is true
#
#
#
#
#
tb %>%
filter(!(!!colname)) # rows where col2 is false
#
#
#
#
#
I could not, however, figure out how to do the same when the column names are stored in rows. For instance:
colname = "col1"
tb %>%
filter(!!colname)
colname = quo("col1")
tb %>%
filter(!!colname)
colname = quo(parse(text = "col1"))
tb %>%
filter(!!colname)
So the question is, how do I do this?
Edit: This is not a duplicate of this question , because since the preferred way to perform a non-standard evaluation with dplyr, all functions ending with _ are now deprecated, and it is now recommended to use a neat evaluation scheme.
source
share