subset with pattern

It is possible to do this via

subset(df, select = grepl("1", names(df)))

For automating this as a function, one can use use [ to do the subsetting. Couple that with one of R's regular expression functions and you have all you need.

By way of an example, here is a custom function implementing the ideas I mentioned above.

Subset <- function(df, pattern) {
  ind <- grepl(pattern, names(df))
  df[, ind]
}

Note this does not error checking etc and just relies upon grepl to return a logical vector indicating which columns match pattern, which is then passed to [ to subset by columns. Applied to your df this gives:

> 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

Tags:

R