select.(-all_of(x)) where x is a list of column names, but some are already removed or do not exist

columns_to_remove = c('b', 'c', 'd')

df |> select.(-all_of(columns_to_remove))

In general, the above code works. However, if column c does not exist in df in the first place, it will not work.
How can I remove the columns in the list, similar to %in%?

Try the following:

columns_to_remove = c('b', 'c', 'd')

df |> select(all_of(!names(df) %in% columns_to_remove))
1 Like

try changing all_of to any_of

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.