Skip to content

Instantly share code, notes, and snippets.

@explodecomputer
Last active October 14, 2018 14:36
Show Gist options
  • Save explodecomputer/844f12f528a43e90bd15a77089e4bd44 to your computer and use it in GitHub Desktop.
Save explodecomputer/844f12f528a43e90bd15a77089e4bd44 to your computer and use it in GitHub Desktop.
combinations
n <- 5
a <- combn(c(1:(2*n)), 2)
b <- combn(1:ncol(a), n)
l <- list()
m <- list()
j <- 1
for(i in 1:ncol(b))
{
x <- a[, b[,i]]
if(!any(duplicated(c(x))))
{
l[[j]] <- x
m[[j]] <- b[,i]
j <- j + 1
}
}
library(gtools)
library(dplyr)
fun <- function(n)
{
p <- permutations(n, n, 1:n)
o <- lapply(1:nrow(p), function(x)
{
x <- matrix(p[x,], nrow=2)
x <- apply(x, 2, sort)
x <- x[,order(x[1,])]
return(x)
}) %>% unique
o <- unique(o)
return(o)
}
fun(4)
fun(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment