Let’s say I have a specific string in R, say “ABCDEFG”. I can break it into a sequence of say every two characters using the following regex.
<code> strsplit("ABCDEFG", "(?<=(..))", perl = TRUE)
[[1]]
[1] "AB" "CD" "EF" "G"
</code>
<code> strsplit("ABCDEFG", "(?<=(..))", perl = TRUE)
[[1]]
[1] "AB" "CD" "EF" "G"
</code>
strsplit("ABCDEFG", "(?<=(..))", perl = TRUE)
[[1]]
[1] "AB" "CD" "EF" "G"
But I want to split it into a specific sequence. First two characters then next one character, then again two then one and so on.
If my input string is “ABCDEFG” I want “AB” “C” “DE” “F” “G” as output (in last element there is only one element left).
How can I do it. I do not want to count nchar
beforehand as I want to do it dynamically.