Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tidy evaluation when column names are stored in strings

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.

like image 861
Luiz Rodrigo Avatar asked Jul 21 '17 16:07

Luiz Rodrigo


1 Answers

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
like image 189
akrun Avatar answered Oct 23 '22 10:10

akrun