Create conjunction of expressions from column list

132 views Asked by At

I want to create a conjunction expression from a char vector. i.e. given a vector like c("a", "b", "c"). Ultimately I want to pass it to dplyr::filter

Here is my attempt:

makeNonNAConjunctionExpr = function(predictors) {
    # Given a char vector c("a", "b", "c") this function returns an expression:
    # ~!is.na(a) & !is.na(b) & !is.na(c)
    f = function(init, i) {
        i_name = as.name(i); 
        if(is.null(init) || length(init) == 0) {
            return(expr(!is.na(!!i_name))) 
        }
        init = if(is_call(init)) {
                    init
                } else {
                    expr(!!as.name(init))
                }
        e = expr(!is.na(!!init) & !is.na(!!i_name)); 
        return(e)
    }
    ex = Reduce(f, predictors[1], predictors[-1])
    return(quo(ex)) # Return a quosure for dplyr.
}

> x = data.frame(a=c(1,NA, 2), b = c(10, 12, NA), c = c(NA, 90, 100))

> e = makeNonNAConjunctionExpr(c("a", "b"))
> e
<quosure: local>
~!is.na(b) & !is.na(a)
> a %>% dplyr::filter(!!e)
  a  b  c
1 1 10 NA

There has to be an easier way in R to create such expressions from strings, but I could not find any. Any suggestions?

0

There are 0 answers