Home > Enterprise >  Get a list of paired vectors from one
Get a list of paired vectors from one

Time:11-15

I have a list with vectors

list_v <- list(c(1,2,3,4,5,6),c(7,8,9),c(77,88,99,100))

I would like to convert it and get paired combinations of all vectors into one list

What I want to get:

list_v_n<- list(c(1,2),c(1,3),c(1,4),c(1,5),c(1,6),c(2,3),c(2,4),c(2,5),c(2,6),c(3,4),c(3,5),c(3,6),c(4,5),c(4,6),c(5,6),c(7,8),c(7,9),c(8,9),c(77,88),c(77,99),c(77,100),c(88,99),c(88,100),c(99,100))

CodePudding user response:

Using combn.

unlist(lapply(list_v, combn, 2, simplify=FALSE), recursive=FALSE)
# [[1]]
# [1] 1 2
# 
# [[2]]
# [1] 1 3
# 
# [[3]]
# [1] 1 4
# 
# [[4]]
# [1] 1 5
# 
# [[5]]
# [1] 1 6
# 
# [[6]]
# [1] 2 3
# 
# [[7]]
# [1] 2 4
# 
# [[8]]
# [1] 2 5
# 
# [[9]]
# [1] 2 6
# 
# [[10]]
# [1] 3 4
# 
# [[11]]
# [1] 3 5
# 
# [[12]]
# [1] 3 6
# 
# [[13]]
# [1] 4 5
# 
# [[14]]
# [1] 4 6
# 
# [[15]]
# [1] 5 6
# 
# [[16]]
# [1] 7 8
# 
# [[17]]
# [1] 7 9
# 
# [[18]]
# [1] 8 9
# 
# [[19]]
# [1] 77 88
# 
# [[20]]
# [1] 77 99
# 
# [[21]]
# [1]  77 100
# 
# [[22]]
# [1] 88 99
# 
# [[23]]
# [1]  88 100
# 
# [[24]]
# [1]  99 100
  • Related