Skip to content
Prev 325092 / 398503 Next

How to expand.grid with string elements (the half!)

Perhaps the OP wants the unique combinations of V1 and V2, as in
  R> d <- expand.grid(V1=c("x","y","z"),V2=c("x","y","z"))
  R> d[ as.numeric(d$V1) <= as.numeric(d$V2), ]
    V1 V2
  1  x  x
  4  x  y
  5  y  y
  7  x  z
  8  y  z
  9  z  z
or
  R> V <- letters[24:26]
  R> rbind(t(combn(V,m=2)), cbind(V,V))
       V   V  
  [1,] "x" "y"
  [2,] "x" "z"
  [3,] "y" "z"
  [4,] "x" "x"
  [5,] "y" "y"
  [6,] "z" "z"

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com