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
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.
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.