0

I've got a dataframe with numeric values between 0 and 1. I'd like to use if_any to do numeric value filtering:

df <- data %>%
filter(if_any(everything(), . > 0.5)

This spits out an error instead:

Error: Problem with `filter()` input `..1`.
ℹ Input `..1` is `if_any(everything(), . < 0.5)`.
x Problem with `across()` input `.fns`.
ℹ `.fns` must be NULL, a function, a formula, or a list of functions/formulas.

Does anyone have a working way of doing this filtering in a tidy way?

Thanks!

4

3 回答 3

0

您可以使用filter_if

data(iris)
iris %>% filter_if(is.numeric, ~ .x < .5)

这将根据您定义的条件过滤数据集的所有数字列,此处 < .5

于 2021-10-15T13:18:31.850 回答
0

我们也可以rowSumsfilter

library(dplyr)
data %>%
    filter(rowSums(. > 0.5) > 0)
于 2021-10-15T17:53:23.797 回答
0

你真的很亲近。您收到的错误消息是因为您忘记了波浪号~

df <- data %>%
        filter(if_any(everything(), ~ . > 0.5)

我可能会建议添加一个额外的列选择,您只将您的标准应用于数字列(否则,如果您的数据框中有字符或因子变量,您将收到错误消息):

df <- data %>%
        filter(if_any(where(is.numeric), ~ . > 0.5)
于 2021-10-15T16:03:21.807 回答