I want to create a subset of my data by using the select and filter functions from dplyr. I have consulted a few similar questions about partial string matches and selecting with grepl, but found no solution to my problem.
The columns that I want to filter all start with the same letters, let's say "DGN." So I have DGN1, DGN2, DGN3, etc. all the way up until DGN25. The two criteria I want to filter on are contains "C18" and starts with "153".
Ideally, I would want to run a code chunk that looks like this:
dgn_subset <- df %>%
select(ID, date, starts_with("DGN") %>%
filter(grepl("C18"|starts_with("153"), starts_with("DGN")))
There are 2 main issues here -- I don't think that grepl can take "starts_with" as an input for the pattern. Also, it can't take "starts_with" as the column argument (I think it may only be able to filter on one column at a time?).
To get the code to work, I could replace the starts_with("153") portion with "153" and the starts_with("DGN") portion with "DGN1," but that gives me many observations that I do not want and it only filters on the first DGN column.
Are there any alternative functions or packages I can use to solve my problem? Any help is greatly appreciated!