I can get the interactions between factors like this: > idx=c(1,3,6,9) > jdx=idx > levels(interaction(idx,jdx,lex.order=TRUE)) [1] "1.1" "1.3" "1.6" "1.9" "3.1" "3.3" "3.6" "3.9" "6.1" "6.3" "6.6" "6.9" [13] "9.1" "9.3" "9.6" "9.9" This list contains all possible interactions. Whereas I need only the combinations, e.g 4 over 2 = 6 total being "1.3 " "1.6" "1.9" "3.6" "3.9" "6.9" I have been unable to find how to get the combinations. Of course with some string manipulations this can be done for this toy example. There should be something in R to make this easy? Thanks in advance Alex van der Spek
Hi Alex, Here is a suggestion: apply(combn(idx, 2), 2, paste, collapse = ".") See ?combn, ?apply and ?paste for more information. HTH, Jorge On Thu, Jun 3, 2010 at 3:08 PM, Alex van der Spek <> wrote:> I can get the interactions between factors like this: > > > idx=c(1,3,6,9) > > jdx=idx > > levels(interaction(idx,jdx,lex.order=TRUE)) > [1] "1.1" "1.3" "1.6" "1.9" "3.1" "3.3" "3.6" "3.9" "6.1" "6.3" "6.6" > "6.9" > [13] "9.1" "9.3" "9.6" "9.9" > > This list contains all possible interactions. Whereas I need only the > combinations, e.g 4 over 2 = 6 total being > > "1.3 " "1.6" "1.9" "3.6" "3.9" "6.9" > > I have been unable to find how to get the combinations. Of course with some > string manipulations this can be done for this toy example. There should be > something in R to make this easy? > > Thanks in advance > Alex van der Spek > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
On Jun 3, 2010, at 2:08 PM, Alex van der Spek wrote:> I can get the interactions between factors like this: > > > idx=c(1,3,6,9) > > jdx=idx > > levels(interaction(idx,jdx,lex.order=TRUE)) > [1] "1.1" "1.3" "1.6" "1.9" "3.1" "3.3" "3.6" "3.9" "6.1" "6.3" "6.6" "6.9" > [13] "9.1" "9.3" "9.6" "9.9" > > This list contains all possible interactions. Whereas I need only the combinations, e.g 4 over 2 = 6 total being > > "1.3 " "1.6" "1.9" "3.6" "3.9" "6.9" > > I have been unable to find how to get the combinations. Of course with some string manipulations this can be done for this toy example. There should be something in R to make this easy? > > Thanks in advance > Alex van der Spek> combn(c(1, 3, 6, 9), 2)[,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 1 1 3 3 6 [2,] 3 6 9 6 9 9 See ?combn HTH, Marc Schwartz
Perhaps not the best or easiest way, but does:> apply(t(combn(idx,2)),1,paste,collapse='.')[1] "1.3" "1.6" "1.9" "3.6" "3.9" "6.9" get you in the right direction? - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Thu, 3 Jun 2010, Alex van der Spek wrote:> I can get the interactions between factors like this: > >> idx=c(1,3,6,9) >> jdx=idx >> levels(interaction(idx,jdx,lex.order=TRUE)) > [1] "1.1" "1.3" "1.6" "1.9" "3.1" "3.3" "3.6" "3.9" "6.1" "6.3" "6.6" "6.9" > [13] "9.1" "9.3" "9.6" "9.9" > > This list contains all possible interactions. Whereas I need only the > combinations, e.g 4 over 2 = 6 total being > > "1.3 " "1.6" "1.9" "3.6" "3.9" "6.9" > > I have been unable to find how to get the combinations. Of course with some > string manipulations this can be done for this toy example. There should be > something in R to make this easy? > > Thanks in advance > Alex van der Spek > > ______________________________________________ > 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. >