all possible combinations of vector lists


citizen

I have a list of vectors and I want to get a list of all possible combinations between the elements of each vector, i.e. combinations of n elements (from a vector) taken two or more at a time.

For example, I have the following list:

> DF
$`1`
   A B   C
1 11 2 432

$`2`
   A B   C
2 11 3 432

$`3`
   A B   C
3 13 4 241

Here is my code:

> d=list()
>   for (j in 1:length(DF)){
+     for (i in 2:length(DF)){
+       d[[j]]=combn(DF[[j]],i,simplify=F)
+     }
+   }
> d
[[1]]
[[1]][[1]]
   A B   C
1 11 2 432

[[2]]
[[2]][[1]]
   A B   C
2 11 3 432

[[3]]
[[3]][[1]]
   A B   C
3 13 4 241

This is wrong because I can only get the combination of three elements of three at the same time. I will have to add combinations of three elements, two at a time. I just get the last loop value. This is a problem with the inner size of the loop.

If I just run the loop for i=2, I get:

> d
[[1]]
[[1]][[1]]
   A B
1 11 2

[[1]][[2]]
   A   C
1 11 432

[[1]][[3]]
  B   C
1 2 432


[[2]]
[[2]][[1]]
   A B
2 11 3

[[2]][[2]]
   A   C
2 11 432

[[2]][[3]]
  B   C
2 3 432

....
Daniel

You can try

lapply(2:3, function(k) { lapply(1:length(DF),function(x){ combn(DF[[x]],k, 
                          simplify = FALSE)})})

Related


all possible combinations of vector lists

citizen I have a list of vectors and I want to get a list of all possible combinations between the elements of each vector, i.e. combinations of n elements (from a vector) taken two or more at a time. For example, I have the following list: > DF $`1` A B

all possible combinations in the vector

Reasonably_exogenous Please assume that I have the following vector: vec = c("A", "B", "C") I would like to be able to generate a vector with the following output: vec_combn = c("", "A", "B", "C", "A+B", "A+C", "B+C", "A+B+C") Here are all possible combinati

make all possible combinations of lists

Dean: I need to be able to create a list containing all possible combinations of input lists. For example, a list [1,2,3]should return [1 [1,2] [1,3] 2 [2,3] 3 [1,2,3]]the list does not have to be in any particular order. On this site I found many functions th

all possible combinations of two lists

wei Given I have two lists: val ints = listOf(0, 1, 2) val strings = listOf("a", "b", "c") I want all possible combinations of their elements 0a, 1a, 2a, 0betc Is there a more elegant way than: ints.forEach { int -> strings.forEach { string ->

make all possible combinations of lists

Dean: I need to be able to create a list containing all possible combinations of input lists. For example, a list [1,2,3]should return [1 [1,2] [1,3] 2 [2,3] 3 [1,2,3]]the list does not have to be in any particular order. On this site I found many functions th

all possible combinations of two lists

wei Given I have two lists: val ints = listOf(0, 1, 2) val strings = listOf("a", "b", "c") I want all possible combinations of their elements 0a, 1a, 2a, 0betc Is there a more elegant way than: ints.forEach { int -> strings.forEach { string ->

make all possible combinations of lists

Dean: I need to be able to create a list containing all possible combinations of input lists. For example, a list [1,2,3]should return [1 [1,2] [1,3] 2 [2,3] 3 [1,2,3]]the list does not have to be in any particular order. On this site I found many functions th

all possible combinations of two lists

wei Given I have two lists: val ints = listOf(0, 1, 2) val strings = listOf("a", "b", "c") I want all possible combinations of their elements 0a, 1a, 2a, 0band many more Is there a more elegant way than: ints.forEach { int -> strings.forEach { string ->

all possible combinations of two lists

wei Given I have two lists: val ints = listOf(0, 1, 2) val strings = listOf("a", "b", "c") I want all possible combinations of their elements 0a, 1a, 2a, 0band many more Is there a more elegant way than: ints.forEach { int -> strings.forEach { string ->

all possible combinations of k with lists of size n

Theodore Narliyski: I want to get all possible combinations of size K from a list of size N. I have a list with "person" objects and I am trying to create a new ArrayList which will be filled with the list of objects. Each table will be a different combination

Find all possible combinations of two lists

Mike V Given two lists of arbitrary length: list1 = ['a', 'b', 'c'] list2 = ['1', '2', '3'] list1is a list of objects, and list2is the possible value for each object. How to find all possible combinations of these two lists (I'm not sure if this is the correc

Find all possible combinations of lists in VBA

username I'm trying to organize a list of data "a,b,c,d,e,...." into all possible combinations of 1,2,3....n elements. E.g: a,b,c,d,e a b c d e a,b a,c a,d a,e b,c b,d d,e ... etc. So far I've only met people who wrote code that found a combination of two set

count of all possible combinations from multiple lists

JDraper I have the following list: list_a = set(["A", "B", "C", "D", "E", "F"]) list_b = set(["1", "2", "3", "4", "5", "6"]) list_c = set(["red", "yellow", "blue", "green"]) I want to find the total number of possible combinations of these lists (one item p

Generate all possible combinations from multiple lists

Merrim Salam, I want to get a list of combinations representing each unique combination of all values in my list of lists. For example, there is a list with a list of sessions, and I want the combination of each session list: My List of lists { List of

Generate all possible combinations from multiple lists

Merrim Salam, I want to get a list of combinations representing each unique combination of all values in my list of lists. For example, there is a list with a list of sessions, and I want the combination of each session list: My List of lists { List of

count of all possible combinations from multiple lists

JDraper I have the following list: list_a = set(["A", "B", "C", "D", "E", "F"]) list_b = set(["1", "2", "3", "4", "5", "6"]) list_c = set(["red", "yellow", "blue", "green"]) I want to find the total number of possible combinations of these lists (one item p

Find all possible combinations of two lists

Mike V Given two lists of arbitrary length: list1 = ['a', 'b', 'c'] list2 = ['1', '2', '3'] list1is a list of objects, and list2is the possible value for each object. How to find all possible combinations of these two lists (I'm not sure if this is the correc

Find all possible combinations of two lists

Mike V Given two lists of arbitrary length: list1 = ['a', 'b', 'c'] list2 = ['1', '2', '3'] list1is a list of objects, and list2is the possible value for each object. How to find all possible combinations of these two lists (I'm not sure if this is the correc

all possible combinations of items in n lists

Bernat Ibanez I need to develop a list that contains all possible combinations in order of elements in n lists. Basically, I'm trying to find all possible paths that will be needed later in another part of the program. I've written some simple code for the two

Generate all possible combinations from multiple lists

Merrim Salam, I want to get a list of combinations representing each unique combination of all values in my list of lists. For example, there is a list with a list of sessions, and I want the combination of each session list: My List of lists { List of

Python 3.3: all possible combinations of lists

Michal I have a list like this: [['one', 'two', 'three', ...], ['a', 'b', ...], ['left', 'right'] ...] and I need to create all possible combinations of this item and put it into a string like: "one|a|left" "one|a|right" "one|b|left" "one|b|right" "two|a|left

Find all possible combinations of lists in VBA

username I'm trying to organize a list of data "a,b,c,d,e,...." into all possible combinations of 1,2,3....n elements. E.g: a,b,c,d,e a b c d e a,b a,c a,d a,e b,c b,d d,e ... etc. So far I've only met people who wrote code that found a combination of two set

all possible combinations of k with lists of size n

Theodore Narliyski: I want to get all possible combinations of size K from a list of size N. I have a list with "person" objects and I am trying to create a new ArrayList which will be filled with the list of objects. Each table will be a different combination