Use of across in Filter

Hi
I am new to R and please let me know how to use across in filter function

my code is below

df1 <-read_sas(read_path(a_in,"adsl.sas7bdat"))

df2 <- filter(df1,across((ITTFL == "Y")))

ITTFL is row and values are Y , N and missing
I am gettting below error

df2 <- filter(df1,across((ITTFL == "Y")))
Error in filter():
! Problem while computing ..1 = across((ITTFL == "Y")).
Caused by error in across():
! object 'ITTFL' not found

But below code is working df2 <- filter(df2,(ITTFL == "Y")) , but what is use of across function

Thank you,
Raja.

across function is used (typically in mutate/summarise) where you want to repeat the same operation across several variables. It has no use anywhere if you want to do one operation with one variable like to see if ITTFL equals Y

Hi Thank you for explanation.

Could you please explain below code how it works across in filter.

mutate(across(where(is.character), zap_empty)) %>%
filter(across(!!popfl)=='Y') %>%

Thank you,
Raja.

I recommend you dont try to use that code.

Using `across()` in `filter()` was deprecated in dplyr 1.0.8.
ℹ Please use `if_any()` or `if_all()` instead.

do you have an actual issue involving applying a multicolumn filter(s) for your work ?
if you want help with that. you can provide a reprex, otherwise the documentation includes examples of filters composed of multiple columns. e.g.

iris %>%
  filter(if_any(ends_with("Width"), ~ . > 4))
iris %>%
  filter(if_all(ends_with("Width"), ~ . > 2))

the first will filter to keep any row in which a column ending with Width in the name has a value greater than 4 , and the second will filter to keep any row where all columns ending Width in their name have values greater than 2

1 Like

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.