I want to use filter()
from {dplyr}
using selection helpers from {tidyselect}
. For example, I may run into a situation where I have either of the following three data frames:
df_1 <-
tribble(~identification_number, ~value,
1, "a",
2, "b",
3, "c",
4, "d",
5, "e")
df_2 <-
tribble(~identification_hash, ~value,
1, "f",
2, "g",
3, "h",
4, "i",
5, "j")
df_3 <-
tribble(~identification_WHATEVER, ~value,
1, "t",
2, "u",
3, "v",
4, "w",
5, "x")
If I have the identification
values for which I want to filter any given data:
ids_to_keep <- c(2, 4, 5)
then the manual (yet undesired) way is to tackle each one such that:
df_1 %>%
filter(identification_number %in% ids_to_keep)
df_2 %>%
filter(identification_hash %in% ids_to_keep)
df_3 %>%
filter(identification_WHATEVER %in% ids_to_keep)
But since I can't anticipate the exact name that will appear in the data's colname, I need a robust way that says: filter the data according to the column that starts with "identification".
From this answer I found that the following works great:
df_1 %>% # or df_2 or df_3
filter_at(vars(starts_with("identification")), all_vars(. %in% ids_to_keep))
However, filter_at()
is superseded. What is the canonical way to do the same in dplyr v 1.0.7?