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.