I have a dataset that looks like the following:
.t0
and .t1
)this
and
This works for your example:
dat[c("id", grep("(this|that)\\d+[a-z]?\\.", names(dat), value = TRUE))]
where:
\\d+
is for one or more digits[a-z]?
is for zero or one lowercase letter\\.
is for the dotIf you want to build a pattern dynamically for various scales
, you can do:
scales <- c("this", "that")
pattern <- sprintf("(%s)\\d+[a-z]?\\.", paste(scales, collapse = "|"))
dat[c("id", grep(pattern, names(dat), value = TRUE))]