df |> filter.(!!!column_name_as_string %in% some_list) not working as intended

df = data.frame(aaa = c(1, 2, 3),
                bbb = c('b', 'c', 'd'),
                ccc = c(5, 6, 7))

column_to_filter = 'bbb'

list_bbb_values_to_filter_rows = as.data.frame(c('b', 'c')) |> pull.()

# Wrong code -- results in empty dataframe
df |> filter.(!!!column_to_filter %in% list_bbb_values_to_filter_rows)

What I would like as a result is

  aaa bbb ccc
    3   d   7

I tried using only one ! but the problem is

Warning message:
In if (!is.na(list_customers_filter)) { :
  the condition has length > 1 and only the first element will be used

I also tried sym() without much luck, and I also tried {{}}

df |> filter.(!{{column_to_filter}} %in% list_bbb_values_to_filter_rows)

and I would like to use all of the elements as filter. How can I filter properly?

using rlang we convert to a symbol and then evaluate that against the %in% condition

df |> filter(!!sym(column_to_filter) %in% list_bbb_values_to_filter_rows)

Thanks, but it seems that this returns the following:

  aaa bbb ccc
    1   b   5
    2   c   6

But I would like it to return this:

  aaa bbb ccc
    3   d   7

Adding extra ! doesn't seem to work.

df |> filter(! (!!sym(column_to_filter) %in% list_bbb_values_to_filter_rows))
1 Like

This topic was automatically closed 7 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.