Levi Waldron
2009-Feb-12 17:20 UTC
[R] 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" > -- Levi Waldron post-doctoral fellow Jurisica Lab, Ontario Cancer Institute Division of Signaling Biology IBM Life Sciences Discovery Centre TMDT 9-304D 101 College Street Toronto, Ontario M5G 1L7
Wills, Kellie
2009-Feb-12 17:40 UTC
[R] getting all pairwise combinations of elements in a characterstring
How about m <- outer(mystring, mystring, paste, sep="") m[upper.tri(m)] Kellie Wills -----Original Message----- From: r-help-bounces at r-project.org on behalf of Levi Waldron Sent: Thu 2/12/2009 12:20 PM To: r-help at r-project.org Subject: [R] getting all pairwise combinations of elements in a characterstring 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" > -- Levi Waldron post-doctoral fellow Jurisica Lab, Ontario Cancer Institute Division of Signaling Biology IBM Life Sciences Discovery Centre TMDT 9-304D 101 College Street Toronto, Ontario M5G 1L7 ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
jim holtman
2009-Feb-12 18:07 UTC
[R] getting all pairwise combinations of elements in a character string
> combn(letters[1:5], 2)[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] "a" "a" "a" "a" "b" "b" "b" "c" "c" "d" [2,] "b" "c" "d" "e" "c" "d" "e" "d" "e" "e">On Thu, Feb 12, 2009 at 12:20 PM, Levi Waldron <lwaldron at uhnres.utoronto.ca> wrote:> 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" >> > > > -- > Levi Waldron > post-doctoral fellow > Jurisica Lab, Ontario Cancer Institute > Division of Signaling Biology > IBM Life Sciences Discovery Centre > TMDT 9-304D > 101 College Street > Toronto, Ontario M5G 1L7 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Levi Waldron
2009-Feb-12 18:39 UTC
[R] getting all pairwise combinations of elements in a character string
I like both of these solutions much better - thank you! -Levi -- Levi Waldron post-doctoral fellow Jurisica Lab, Ontario Cancer Institute Division of Signaling Biology IBM Life Sciences Discovery Centre TMDT 9-304D 101 College Street Toronto, Ontario M5G 1L7