I have an object containing a text string:
x <- "xxyyxyxy"
and I want to split that into a vector with each element containing two letters:
[1] "xx" "yy" "xy" "xy"
It seems like the strsplit
should be my ticket, but since I have no regular expression foo, I can't figure out how to make this function chop the string up into chunks the way I want it. How should I do this?
Using C++ one can be even faster. Comparing with GSee's version:
How about
Basically, add a separator (here " ") and then use
strsplit
Here's one way, but not using regexen:
Here is one option using
stringi::stri_sub()
. Try:ATTENTION with substring, if string length is not a multiple of your requested length, then you will need a +(n-1) in the second sequence:
From my testing, the code below is faster than the previous methods that were benchmarked. stri_sub is pretty fast, and seq.int is better than seq. It's also easy to change the size of the strings by changing all the 2Ls to something else.
I didn't notice a difference when string chunks were 2 characters long, but for bigger chunks this is slightly better.