简体   繁体   English

如何使用多种模式在 R 中执行 filter_all

[英]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.我需要帮助过滤 R 中整个数据帧的一组值。 I am using filter_all which works for a single value, but when I use a vector, it fails.我正在使用适用于单个值的 filter_all,但是当我使用向量时,它会失败。 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) .使用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. %in%像集合操作一样工作。 Compare 1:4 == c(1, 3) vs 1:4 %in% c(1, 3) :比较1:4 == c(1, 3)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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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