paradoxical behavior in filter and if_any

I am trying to find rows via if_any and have run into a very strange behavior. Is this a bug?

my_DF <- data.frame( a = c(1, 1, NA, 1),
b = c(1, NA, 1, 12))
my_DF

my_DF %>%
filter( if_any(everything(), ~ .x == 12 && .y==1))
my_DF %>%
filter( if_any(everything(), ~ .x == 1 && .y==12))

the first filter returns an empty dataframe, which is correct. The second filter causes an error that I can't understand.

Simplifying the code:

my_DF <- data.frame( a = c(1, 1, NA, 1),
b = c(1, NA, 1, 12))
my_DF

my_DF %>%
filter( if_any(everything(), ~ .x == 1 && .y==12))

Still produces the error.

I am new to the syntax involving if_any so maybe I am making some sort of beginners error?

Any help appreciated!

Carl

You should say what your intent is / what you believe the behaviour should be ...

It seems like you think you should inherently have access to analyse pairs of columns, (.x .y ) being shortcuts to them... but you've not set up anything like that.
my_df happens to have 2 columns, but it might have had any number, 1,2,3....
everything() would mean you are processing those 'any number of columns' and you can apply a test on the contents of them (.x == etc.) individually, then if_any will then be true if any of the columns test true. effectively combining them at that level for the filter.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.