I wish to share an R function for finding all possible unique undirected combinations between elements of a single vector:
combi <- function(vec1)
{
si <- length(vec1)
first <- rep(vec1, (si-1):0)
secR <- rev(vec1)
second <- secR[sequence(1:(si-1))]
second <- rev(second)
combi <- matrix(cbind(first, second), ncol = 2)
return(combi)
}
and ask if there is a simpler way of doing this? (I need the result to be in a 2-column matrix).
Well, there’s a built-in
combnfunction:Yours looks faster, though, perhaps because
combnis trying to solve a more general problem (??):