gpt4 book ai didi

r - 在 dplyr 中跨列过滤

转载 作者:行者123 更新时间:2023-12-05 09:29:59 25 4
gpt4 key购买 nike

我想使用过滤器和跨函数过滤 iris 数据框,只返回 sepal.length、sepal.width、petal.length 和 petal.width 字段中值大于 2 的行。我有以下代码:

iris%>%
filter(across(c(Sepal.Length, Sepal.Width , Petal.Length, Petal.Width), >2))

报错信息是:Error: unexpected '>' in:

谁能建议修改代码来解决这个问题?

最佳答案

一个可能的解决方案,基于dplyr:

library(dplyr)

iris%>%
filter(across(is.numeric, ~ .x > 2))

或者:

iris%>%
filter(across(c(Sepal.Length,Sepal.Width,Petal.Length,Petal.Width), ~ .x > 2))

甚至:

iris%>%
filter(across(ends_with(c("Length","Width")), ~ .x > 2))

关于r - 在 dplyr 中跨列过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70097359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com