string starting with specific pattern

后端 未结 2 568
终归单人心
终归单人心 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

    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.

提交回复
热议问题