This is an "R" question:
suppose I have a vector of 3 letters for example: "BBSSHHSRBSBBS" what I like to find is the position of the first "S" that appears after "B" sequence. For example in the vector above the first "S" that appears after "B" sequences will appear in the 3-rd place the 10-th place and the last place(13)
I can do trivially using loops but I like to find out if there is any way to do it in "R" without looping at all.
The function should get an R vector as an input and return the vector of "S" positions as an output
Thanks,
Another base R solution
str <- "BBSSHHSRBSBBS"
pos <- unlist(gregexpr("BS", str))
Note that gregexpr
accepts regular expressions so you can catch much more complex patterns.
Maybe with str_locate_all:
library(stringr)
v <- "BBSSHHSRBSBBS"
str_locate_all(v, "BS")
[[1]]
start end
[1,] 2 3
[2,] 9 10
[3,] 12 13
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With