Christopher Penn
Christopher Penn

Reputation: 539

dplyr if_any and numeric filtering?

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!

Upvotes: 3

Views: 5945

Answers (3)

akrun
akrun

Reputation: 887511

We may also use rowSums in filter

library(dplyr)
data %>%
    filter(rowSums(. > 0.5) > 0)

Upvotes: 0

LMc
LMc

Reputation: 18672

You're really close. The erorr message you've gotten is because you forgot the tilde ~:

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

I might suggest adding an additional column selection where you only apply your criteria to numeric columns (otherwise you will get an error if you have character or factor variables in your data frame):

df <- data %>%
        filter(if_any(where(is.numeric), ~ . > 0.5)

Upvotes: 3

Damien Georges
Damien Georges

Reputation: 126

You can use filter_if:

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

This will filter all the numeric column of your dataset according to the condition you define, here < .5

Upvotes: 0

Related Questions