subset with pattern

前端 未结 3 1244
终归单人心
终归单人心 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
    
    0 讨论(0)
  • 2020-12-19 06:54

    Base R now has a convenience function endsWith():

    df[, endsWith(names(df), "1")]
    

    In data.table you can do:

    library(data.table)
    setDT(df)
    df[, .SD, .SDcols = patterns("1")]
    # Or more precisely
    df[, .SD, .SDcols = patterns("1$")]
    

    In dplyr:

    library(dplyr)
    select(df, contains("1"))
    # Or more precisely
    select(df, ends_with("1"))
    
    0 讨论(0)
  • 2020-12-19 06:57

    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
    
    0 讨论(0)
提交回复
热议问题