Skip to content
Prev 170286 / 398503 Next

getting all pairwise combinations of elements in a character string

I'm able to do this as follows, but am wondering if anyone knows a 
simpler way which still avoids explicit loops?

 > (mystring <- letters[1:5])
[1] "a" "b" "c" "d" "e"
 > unlist(sapply(mystring[-length(mystring)],
+               function(x) 
paste(x,mystring[(grep(x,mystring)+1):length(mystring)],sep="")))
  a1   a2   a3   a4   b1   b2   b3   c1   c2    d
"ab" "ac" "ad" "ae" "bc" "bd" "be" "cd" "ce" "de"
 >