unique pairs from expand.grid




#-------------------------

# SAMPLE DATA
set.seed(123)
wordLength <- 3
N <- 500
stringVec <- apply(replicate(wordLength, sample(LETTERS, N, TRUE), TRUE), 1, paste, collapse="")
#-------------------------



  pairwise <- expand.grid(stringVec, stringVec, stringsAsFactors = FALSE)

justUniquePairs.01 <- function(pairwise) {
    pairwise <- pairwise[pairwise[, 1] != pairwise[, 2], ]
    pairwise[as.logical(apply(pairwise, 1, order)[1, ]-1), ]
}


justUniquePairs.02 <- function(pairwise) {
    # one line, gets rid of the equal-valued pairs too
    pairwise[pairwise[, 1] > pairwise[, 2], ]
}


U01 <- quote(justUniquePairs.01(pairwise))
U02 <- quote(justUniquePairs.02(pairwise))

mbench(U01=U01, U02=U02)

Times for a singe run are: 
    U01   U02
1 3.264 0.076

  Microbenchmark will run 5 reps


Unit: milliseconds
 expr        min         lq     median         uq        max neval
  U01 3237.19696 3357.37661 3416.35548 3503.40999 3773.48550     5
  U02   77.37778   78.99819   79.42047   79.54704   80.21823     5
> 
