I have a column with strings; I am trying to get counts if an alphabet occurs successively.
j <- data.frame(states= c("AYYOOYYYYYZ", "AYYCCYYYYYZ", "AYYCOOYYCCZ"))
I used str_count
; it returns 3 3 2 as counts for YY instead of 5 5 2.
I tried regex, grepexr but couldn't get counts recursively.
str_count(j$states, "YY")
[1] 3 3 2
expected output:
structure(list(states = c("AYYOOYYYYYZ", "AYYCCYYYYYZ", "AYYCOOYYCCZ"
), rec_yy = c(5, 5, 2), rec_oo = c(1, 0, 1), rec_cc = c(0, 1,
1)), row.names = c(NA, -3L), class = "data.frame")
I appreciate your help!