tags:

views:

468

answers:

5

I have an object containing a text string:

x <- "xxyyxyxy"

and I want to turn 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 the way I want it. How should I do this?

+2  A: 

Total hack, JD, but it gets it done

x <- "xxyyxyxy"
c<-strsplit(x,"")[[1]]
sapply(seq(2,nchar(x),by=2),function(y) paste(c[y-1],c[y],sep=""))
[1] "xx" "yy" "xy" "xy"
DrewConway
That's exactly the hack I was coding up. of course I was going to do a loop instead of sapply ;)
JD Long
+3  A: 

Here's one way, but not using regexen:

a <- "xxyyxyxy"
n <- 2
sapply(seq(1,nchar(a),by=n), function(x) substr(a, x, x+n-1))
Ken Williams
+4  A: 

strsplit is going to be problematic, look at a regexp like this

strsplit(z, '[[:alnum:]]{2}')  

it will split at the right points but nothing is left.

You could use substring & friends

z <- 'xxyyxyxy'  
idx <- 1:nchar(z)  
odds <- idx[(idx %% 2) == 1]  
evens <- idx[(idx %% 2) == 0]  
substring(z, odds, evens)  
geoffjentry
that's a sweet way of doing it as well. I think I let myself get mentally hooked on srtsplit() because of how close strsplit(x,"") is to what I want.
JD Long
+6  A: 

How about

strsplit(gsub("([[:alnum:]]{2})", "\\1 ", x), " ")[[1]]

Basically, add a separator (here " ") and then use strsplit

seth
I like this one a lot
geoffjentry
Nice job, I like this one alot too!
Jay
+5  A: 

Using substring is the best approach:

substring(x, seq(1,nchar(x),2), seq(2,nchar(x),2))

But here's a solution with plyr:

laply(seq(1,nchar(x),2), function(y) substr(x, i, i+1))
Shane