I need to filter a table by a logical column (or, more precisely, by its negation), but the name of the column may vary. It's easy when I know their names beforehand:
tb = tibble(
id = 1:4,
col1 = c(TRUE, TRUE, FALSE, FALSE),
col2 = c(TRUE, FALSE, TRUE, FALSE)
)
tb
## # A tibble: 4 x 3
## id col1 col2
## <int> <lgl> <lgl>
## 1 1 TRUE TRUE
## 2 2 TRUE FALSE
## 3 3 FALSE TRUE
## 4 4 FALSE FALSE
colname = quo(col1)
tb %>%
filter(!!colname) # rows where col1 is true
## # A tibble: 2 x 3
## id col1 col2
## <int> <lgl> <lgl>
## 1 1 TRUE TRUE
## 2 2 TRUE FALSE
tb %>%
filter(!(!!colname)) # rows where col1 is false
## # A tibble: 2 x 3
## id col1 col2
## <int> <lgl> <lgl>
## 1 3 FALSE TRUE
## 2 4 FALSE FALSE
colname = quo(col2)
tb %>%
filter(!!colname) # rows where col2 is true
## # A tibble: 2 x 3
## id col1 col2
## <int> <lgl> <lgl>
## 1 1 TRUE TRUE
## 2 3 FALSE TRUE
tb %>%
filter(!(!!colname)) # rows where col2 is false
## # A tibble: 2 x 3
## id col1 col2
## <int> <lgl> <lgl>
## 1 2 TRUE FALSE
## 2 4 FALSE FALSE
I could not, though, figure out how to do the same when the column names are stored in strings. For instance:
colname = "col1"
tb %>%
filter(!!colname)
## Error in filter_impl(.data, quo): Argument 2 filter condition does not evaluate to a logical vector
colname = quo("col1")
tb %>%
filter(!!colname)
## Error in filter_impl(.data, quo): Argument 2 filter condition does not evaluate to a logical vector
colname = quo(parse(text = "col1"))
tb %>%
filter(!!colname)
## Error in filter_impl(.data, quo): Argument 2 filter condition does not evaluate to a logical vector
So, the question is, how should I do it?
Edit: This is not a duplicate of this question because the preferred way to do non-standard evaluation with dplyr has changed since then. All functions terminated in _ are now deprecated, and the tidy evaluation framework is recommended now.
We can use sym
from rlang
for evaluating strings
library(rlang)
library(dplyr)
colname <- "col1"
tb %>%
filter(!!sym(colname))
# A tibble: 2 x 3
# id col1 col2
# <int> <lgl> <lgl>
#1 1 TRUE TRUE
#2 2 TRUE FALSE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With