Skip to content

ordering x's and y's

2 messages · Andrew Criswell, robin hankin

#
Hello ALL:

How do I get R to list all possible orderings of 2 x's and 3 y's?  It should
look like this (which rows appear first is unimportant):

x x y y y
x y x y y
x y y x y
x y y y x
y x x y y
y x y x y
y x y y x
y y x x y
y y x y x
y y y x x

Thanks,
ANDREW
#
Hi Andrew.

This was asked a few days ago (but I posted my offering offline)..
Try:

library(gregmisc)
do.thing2 <- function(x,y) {
   a <- c(x,y)
   tt <- combinations(length(a),length(x))
   answer <- matrix(NA,nrow(tt),length(a))
   for(i in 1:nrow(tt)) {
     answer[i, tt[i,]] <- x
     answer[i,-tt[i,]] <- y
   }
return(answer)
}

[anyone got a vectorized version?]


best

rksh