Home > Software design >  dplyr if_any and numeric filtering?
dplyr if_any and numeric filtering?

Time:10-16

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!

CodePudding user response:

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

CodePudding user response:

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)

CodePudding user response:

We may also use rowSums in filter

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