首页 > 解决方案 > filter_all 给了我相同的未过滤数据框

问题描述

我试图仅查找当前诊断并过滤到任何大于 2 的值。我认为我不一定会以正确的方式进行此操作,但这是我现在拥有的代码行。通过我所写的内容,我刚刚返回了一个名为 FU 的数据帧,它与 FU 完全相同。不完全确定我的问题从哪里来。

标签: r

解决方案


我们需要filter_at而不是filter_all

library(dplyr)
fuKSADS_1%>% 
     select(ends_with('Current')) %>%
     filter_all(any_vars( . > 2))

或者另一种选择是c_across

fuKSADS_1 %>%
     select(ends_with('Current')) %>%
     rowwise %>% 
     filter(any(c_across(everything()) > 2)) %>%
     ungroup

base R,我们可以使用Reduce

i1 <- endsWith(names(fuKSADS_1), 'Current')
subset(fuKSADS_1[i1], Reduce(`|`, lapply(fuKSADS_1[i1], `>`, 2)))

推荐阅读