Extract a string between patterns/delimiters in R

前端 未结 4 2006
借酒劲吻你
借酒劲吻你 2020-12-11 22:24

I have variable names in the form:

PP_Sample_12.GT

or

PP_Sample-17.GT

I\'m trying to use string split to

4条回答
  •  清歌不尽
    2020-12-11 22:29

    This grabs the 2nd element of each part of the list that was split and then simplifies it into a vector by subsetting the function [, using sapply to call this function for each element of the original list.

    x <- c('PP_Sample_12.GT', 'PP_Sample-17.GT')
    sapply(strsplit(x, '(?:_(?=\\D)|\\.GT)', perl = T), '[', 2)
    
    [1] "Sample_12" "Sample-17"
    

提交回复
热议问题