Search code examples
rfunctional-programmingmagrittr

Exclude multiple values in a vector in a pipe


I am looking for a more elegant way to exclude multiple values from a vector in a pipe. The code below achieves what I need, but is quite ugly. Could you think of an alternative?

values_to_exclude <- c("b", "d")
        
letters[1:5] %>%
    .[ !(. %in% values_to_exclude) ] # quite ugly

Solution

  • This doesn't use purrr, but how about something like this:

    values_to_exclude <- c("b", "d")
    letters[1:5] %>% setdiff(., values_to_exclude)
    # [1] "a" "c" "e"