Remove columns that contain certain words

My dataset has >1000 variables, and I want to remove all that start with the 'b', 'k', 'hw', and end in numbers other than '_01' (there are up to _20). I can remove them one by one using dplyr's select method, but it takes time when there are too many different patterns, so I'd like to know if this can be done in one shot.

Thanks for your suggestions.


yeah, I would try to use select with starts_with to select the columns with the - symbol to specify that you don't want them to be selected

df <- df %>% select(-starts_with("b"), -starts_with("k"), -starts_with("hw"))
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.