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?
strsplit is going to be problematic, look at a regexp like this
it will split at the right points but nothing is left.
You could use substring & friends
A helper function:
Using
substring
is the best approach:But here's a solution with plyr:
Well, I used the following pseudo-code to fulfill this task:
In code, I did
This returns a list with the split vector inside, though, not a vector.
Total hack, JD, but it gets it done
Here is a fast solution that splits the string into characters, then pastes together the even elements and the odd elements.
Benchmark Setup:
Benchmark 1:
Benchmark 2:
Now, with bigger data.