I have a dataframe with the following format. I'm going to subset a dataframe for specific months (Not one month).
#Example:
df <- data.frame ()
df[1, 1:4] <- sample(1:4)
df[2, 1:4] <- sample(1:4)
colnames(df) <- c("X2012.01.01", "X2012.02.01", "X2012.03.01", "X2012.04.01")
I could use the following codes for one month but I don't know why I can't use it for several months. It outputs 0 in the dataframe.
df %>% dplyr::select(contains(".01."))
I used ".01.|.02."
to define two months but I couldn't get the desired result. I don't know how to resolve this issue.
CodePudding user response:
contains
looks for literal strings, it doesn't use regex. Use matches()
for a regex pattern. Do note that .
is a special character in regex, so you might have better luck escaping .
, something like
select(matches("\\.(01|02)\\."))
See ?contains or ?matches for the selection helpers documentation page.
You can also pass a vector to contains, e.g.,
select(contains(c(".01.", ".02.")))