I am looking to use dplyr to select only variables in my dataset that end in "500" exactly. That is, I do not want variables ending in 2500, 5500, or 1500. Just 500. I tried using ends_with("500")
, but this includes the aforementioned variables ending in 2500, etc that I want to exclude.
I'm sure this is a very thing to do, but I am having trouble finding exactly what I want via google search.
Thanks!
CodePudding user response:
We may use matches
instead of ends_with
as ends_with
does a fixed match and it will match the 500 or 1500 etc. Instead, if we use matches
, there is flexibility in it i.e. can specify \\D
before the 500 that it matches non-digits (not clear without a reproducible example though)
library(dplyr)
df1 %>%
select(matches('\\D 500$'))