Tyler R.
Tyler R.

Reputation: 461

dplyr Create new boolean variable from any_vars() filter condition

I would like to create a boolean variable based on a condition of multiple variables. As shown in the second answer to this question, one can filter based on many conditions across a number of columns:

 mtcars %>% filter_at(vars(starts_with("d"),starts_with("c")), any_vars(. %in% c(4)))

I would like to do something like

test <- mtcars %>% mutate(has.4 = any_vars(vars(starts_with("d"),starts_with("c")) %in% c(4)))

but this of course does not work and gives Error: Column ``has.4`` is of unsupported type quoted call.

The filter_at() call is already creating the boolean with which to subset the data on. How can I pipe that output into a new variable, and keep all rows?

Also, I only want to create one new column, so mutate_at() and mutate_if() don't seem to be the right calls.

Upvotes: 2

Views: 1634

Answers (1)

akrun
akrun

Reputation: 887501

Instead of the starts_with, here an option is matches

library(dplyr)
out1 <- mtcars %>% 
            filter_at(vars(matches('^(d|c)')), any_vars(. %in% 4))

If we need a column, we can create the condition with rowSums

out2 <- mtcars %>%
           mutate(has.4 = rowSums(select(., matches("^(d|c)")) == 4) > 0)

identical(out1, out2 %>%
                        filter(has.4) %>%
                         select(-has.4))
#[1] TRUE

Upvotes: 1

Related Questions