[英]Filter in R if there is any value in row between range
我有这个data.frame
V1 V2 V3 V4 V5
<dbl> <dbl> <dbl> <dbl> <dbl>
0.000 0.000 0.000 0.000 1.000
0.651 0.000 0.000 0.000 0.349
0.000 0.000 1.000 0.000 0.000
0.703 0.000 0.297 0.000 0.000
0.018 0.982 0.000 0.000 0.000
0.683 0.000 0.000 0.000 0.317
0.000 0.000 0.000 0.000 1.000
0.000 0.000 0.000 0.000 1.000
0.000 0.001 0.000 0.000 0.999
其中列是给定 class 中的成员概率。我需要过滤具有给定范围之间的任何值的行,比如.30 > x >.70
。
理想的结果是
V1 V2 V3 V4 V5
<dbl> <dbl> <dbl> <dbl> <dbl>
0.651 0.000 0.000 0.000 0.349
0.683 0.000 0.000 0.000 0.317
我尝试data %>% filter(if_any(starts_with("V"), ~. > 0.3) & if_any(starts_with("V"), ~. < 0.7))
但这几乎返回了所有行。
您可以rowwise
过滤所有列并检查您的条件,如下所示:
library(dplyr)
df %>%
rowwise() %>%
filter(any(c_across() > 0.3 & c_across() < 0.7))
#> # A tibble: 2 × 5
#> # Rowwise:
#> V1 V2 V3 V4 V5
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.651 0 0 0 0.349
#> 2 0.683 0 0 0 0.317
创建于 2023-02-01,使用reprex v2.0.2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.