[R] interval between specific characters in a string...

Evan Cooch ev@n@cooch @end|ng |rom gm@||@com
Fri Dec 2 18:18:56 CET 2022

Was wondering if there is an 'efficient/elegant' way to do the following 
(without tidyverse). Take a string


Its easy enough to count the number of times the character 'b' shows up 
in the string, but...what I'm looking for is outputing the 'intervals' 
between occurrences of 'b' (starting the counter at the beginning of the 
string). So, for the preceding example, 'b' shows up in positions

2, 6, 7, 13, 17

So, the interval data would be: 2, 4, 1, 6, 4

My main approach has been to simply output positions (say, something 
like unlist(gregexpr('b', target_string))), and 'do the math' between 
successive positions. Can anyone suggest a more elegant approach?

Thanks in advance...

More information about the R-help mailing list