This can be done using
subset(df, select = grepl("1", names(df)))
To automate this, you can use functions as functions [to execute a subset. Pair that with one of the regex functions R, and you have everything you need.
As an example, this is a user-defined function that implements the ideas mentioned above.
Subset <- function(df, pattern) {
ind <- grepl(pattern, names(df))
df[, ind]
}
, .. grepl, , , pattern, [ . df :
> Subset(df, pattern = "1")
a1 b1
1 1 2
2 2 3
3 3 4
4 4 5
5 5 6
6 6 7
7 7 8
8 8 9
9 9 10
10 10 11