简体   繁体   中英

How to do filter_all in R using multiple patterns

I need help with filtering a set of values across an entire data frame in R. I am using filter_all which works for a single value, but when I use a vector, it fails. The resulting data frame should not contain any values from patterns. Please help.

patterns <-c(-1,-2,-3,-4)

data %>% filter_all(any_vars(. !=patterns))

Use any_vars(. . %in% patterns) . == and != are element-wise, comparing first element to first element, second element to second element, etc. (with recycling, if the lengths don't match). %in% works like a set operation. Compare 1:4 == c(1, 3) vs 1:4 %in% c(1, 3) :

1:4 == c(1, 3)
# [1] TRUE FALSE FALSE FALSE
1:4 %in% c(1, 3)
# [1] TRUE FALSE TRUE FALSE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM