[英]R dplyr filter rows based on conditions from several selected columns
I have a dataframe DF
, and I want to filter it based on condition from several selected columns.我有一个 dataframe
DF
,我想根据几个选定列的条件对其进行过滤。
For instance, I want to filter rows in DF that fulfil the condition that this row contains any values that are smaller than 0.03 in column PCS_AB
, PCS_AD
, PCS_BD
.例如,我想过滤 DF 中满足条件的行,即该行包含列
PCS_AB
、 PCS_AD
、 PCS_BD
中小于 0.03的任何值。
DF <- cbind.data.frame(A = c(100, 10, 13),
B = c(33, 44, 12),
D = c(110, 21, 22),
PCS_AB = c(0.03, 0.001, 0.3),
PCS_AD = c(0.01, 0.2, 0.33),
PCS_BD = c(0.99, 1.0, 0.45))
I can achieve it by the following code:我可以通过以下代码实现它:
DF_filter <- DF %>%
filter(PCS_AB < 0.03 | PCS_AD < 0.03 | PCS_BD < 0.03)
But I want something simpler like the pseudo code as following:但我想要一些更简单的东西,比如下面的伪代码:
DF2 <- DF %>%
filter(any(starts_with("PCS")) < 0.03)
Is it possible with dplyr? dplyr 可以吗? Thanks.
谢谢。
We can use if_any
from the version 1.0.4
of dplyr
我们可以使用
if_any
版本1.0.4
中的dplyr
library(dplyr)
DF %>%
filter(if_any(starts_with("PCS"), ~ . <= 0.03))
-output -输出
# A B D PCS_AB PCS_AD PCS_BD
#1 100 33 110 0.030 0.01 0.99
#2 10 44 21 0.001 0.20 1.00
It is also possible filter_at
with any_vars
(soon to be deprecated)也可以
filter_at
和any_vars
(即将被弃用)
DF %>%
filter_at(vars(starts_with("PCS")), any_vars(. <= 0.03))
-output -输出
# A B D PCS_AB PCS_AD PCS_BD
#1 100 33 110 0.030 0.01 0.99
#2 10 44 21 0.001 0.20 1.00
Or use rowSums
to create the logical vector或使用
rowSums
创建逻辑向量
DF %>%
filter(rowSums(select(., starts_with('PCS')) < 0.03) > 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.