if_any 过滤失败

问题描述 投票:0回答:2

我想过滤所有给定变量值> 2.5的行(我的意思是变量

xyz
)。下面的代码失败了,有人可以帮忙吗?谢谢!

library(tidyverse)
data("diamonds")
diamonds %>% filter(across(where(is.double), ~ if_all(.)>2.5))
r dplyr filter across
2个回答
0
投票
library(dplyr)

diamonds %>%  filter(if_all(where(is.double), ~ . > 2.5))

#> # A tibble: 125 × 10
#>    carat cut       color clarity depth table price     x     y     z
#>    <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
#>  1  3    Very Good H     I1       63.1    55  6512  9.23  9.1   5.77
#>  2  2.72 Fair      J     I1       68.2    56  6870  8.46  8.43  5.76
#>  3  3.01 Premium   I     I1       62.7    58  8040  9.1   8.97  5.67
#>  4  3    Fair      H     I1       67.1    57  8044  8.93  8.84  5.97
#>  5  2.68 Premium   G     I1       58.6    60  8419  9.11  9.07  5.33
#>  6  2.74 Fair      J     I1       64.9    61  8807  8.76  8.66  5.65
#>  7  2.68 Very Good I     I1       63.5    56  9665  8.81  8.77  5.58
#>  8  3.11 Fair      J     I1       65.9    57  9823  9.15  9.02  5.98
#>  9  3.01 Premium   F     I1       62.2    56  9925  9.24  9.13  5.73
#> 10  2.52 Fair      G     I1       66.9    57 10076  8.39  8.33  5.6 
#> # ℹ 115 more rows

创建于 2024-04-12,使用 reprex v2.0.2


-1
投票

如果您只需要 x、y 和 z 列,您可以只过滤这三列吗?

library(tidyverse)
data("diamonds")
diamonds %>% filter(
  x>2.5,
  y>2.5,
  z>2.5)
© www.soinside.com 2019 - 2024. All rights reserved.