Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subset with pattern

Tags:

r

Say I have a data frame df

df <- data.frame( a1 = 1:10, b1 = 2:11, c2 = 3:12 )

I wish to subset the columns, but with a pattern

df1 <- subset( df, select= (pattern = "1") )

To get

> df1
   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

Is this possible?

like image 580
Wet Feet Avatar asked Oct 17 '13 03:10

Wet Feet


2 Answers

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
like image 196
Gavin Simpson Avatar answered Sep 21 '22 23:09

Gavin Simpson


Same same but different:

df2 <- df[, grep("1", names(df))]
   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
like image 42
bathyscapher Avatar answered Sep 20 '22 23:09

bathyscapher