qfazille
5/15/2019 - 2:47 PM

(some) R base funs : combn & expand.grid

(some) R base funs : combn & expand.grid

# Create combinaisons (here of length 2) possibles for elements of vector
elts <- letters[1:5]
as.data.frame(t(combn(elts, m = 2)))

#    V1 V2
# 1   a  b
# 2   a  c
# 3   a  d
# 4   a  e
# 5   b  c
# 6   b  d
# 7   b  e
# 8   c  d
# 9   c  e
# 10  d  e

# If you also want the reverse ones + combinaisons with same letter :
expand.grid(V1 = elts, V2 = elts)

#    V1 V2
# 1   a  a
# 2   b  a
# 3   c  a
# 4   d  a
# 5   e  a
# 6   a  b
# 7   b  b
# 8   c  b
# 9   d  b
# 10  e  b
# 11  a  c
# 12  b  c
# 13  c  c
# 14  d  c
# 15  e  c
# 16  a  d
# 17  b  d
# 18  c  d
# 19  d  d
# 20  e  d
# 21  a  e
# 22  b  e
# 23  c  e
# 24  d  e
# 25  e  e