R: Subset / index an object by substring of the wanted entries

后端 未结 2 1084
生来不讨喜
生来不讨喜 2021-01-23 13:40

Is it possible to extract/subset a dataframe by indicating only a chunk of the wanted entries-string?

The filter criteria is stored in an factor vector. But there are on

2条回答
  •  甜味超标
    2021-01-23 14:31

    A regex approach:

    subset(data, grepl(paste0("^",IDfilter,collapse="|"), ID))
    
            ID sex size
    1  0120010   M    7
    2  0120020   F    6
    3  0121031   F    9
    8  0150030   F   11
    9  0150110   F   12
    11 1150110   F   12
    

    Note: "^" is to match the beginning of the string. I'm assuming there are only digits in your filters.

提交回复
热议问题