subset with pattern

前端 未结 3 1260
终归单人心
终归单人心 2020-12-19 06:32

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

3条回答
  •  长情又很酷
    2020-12-19 06:49

    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
    

提交回复
热议问题