string starting with specific pattern

后端 未结 2 567
终归单人心
终归单人心 2021-01-24 08:19

This is very simple R question but I could not find an answer.

I would like to find strings starting with a specific pattern. e.g. if I have the pattern \"ABC

相关标签:
2条回答
  • 2021-01-24 08:37

    Regex is simpler in this case:

    grepl("^ABC", x)
    [1]  TRUE FALSE FALSE
    

    The caret ^ special character identifies the beginning of the line. No need to have to specify the amount of characters to count to.

    0 讨论(0)
  • 2021-01-24 08:37

    You could use substr for this:

    test <- c("ABCGDFGFD","WWABC","AYBC")
    
    substr(test, 1, 3) == 'ABC'
    
    [1]  TRUE FALSE FALSE
    

    If it needs to be longer or shorter, you can change the arguments in substring from 1 and 3. With 1 and 3, it starts at the beginning of each string, and looks up to, and including, the third character.

    0 讨论(0)
提交回复
热议问题