r - 在 dplyr 中跨列过滤

我想使用过滤器和跨函数过滤 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))

https://stackoverflow.com/questions/70097359/

相关文章:

flutter - 闪闪发光的动画

c++ - `shared_ptr::use_count() == 0` 和 `shared_ptr

javascript - 显示: inline is used时隐藏元素符号

ios - 如何获取 UTType 图像、音频和视频的所有扩展

c++ - 你能在 C++ 中有一个数组 vector 吗?

javascript - 左关联二叉树折叠

ruby-on-rails - 如何一次为 Ruby 中的对象分配多个属性

spring - 如何在调用构造函数之前注入(inject) spring @Value 注释值?

function - 关于在函数和宏定义中使用结构文字

c - 使用 fgets 时未打印出来