Split a list of elements into two unique lists in R (and get all combinations)


Daniel Rollins

I have a list of elements (I have 11 elements in my actual list, this is just an example):

x <- c(1, 2, 3)

and want to split them into two lists (with all the entries), but I want to return all possible combinations of this list like:

(1,2)(3) & (1)(2,3) & (2)(1,3)

Does anyone know a more efficient way to handle more complex lists?

Thanks in advance for your help!

Carlos Consecius

List 3 elements:

vec <- 1:3

Note that for each element we have two possibilities: whether it is in the first split or in the second. Therefore, we define a matrix of all possible splits (row-wise) with expand.gridwhich all possible combinations can be produced:

groups <- as.matrix(expand.grid(rep(list(1:2), length(vec))))

However, this treats the case where the group is flipped to a different split as a treatment. Also includes scenarios where all observations are in the same group (but only 2 of them).

If we want to remove them, we need to remove only one group of rows (2 such rows) from the groupsmatrix and split all the rows of the vector in the same way, just switch the group.

A set of entries is at the top and bottom, so remove them easily:

groups <- groups[-c(1, nrow(groups)),]

Duplicate entries are a little trickier. But note that we can get rid of them by deleting all the lines where the first group is 2. In effect, this would require that the first element is always assigned to group 1.

groups <- groups[groups[,1]==1,]

The job then is to split the list with each row in the groupsmatrix . To do this, we use to Mapcall functions on each row of lists and matrices :split()vecgroups

splits <- Map(split, list(vec), split(groups, row(groups)))

> splits
[[1]]
[[1]]$`1`
[1] 1 3

[[1]]$`2`
[1] 2


[[2]]
[[2]]$`1`
[1] 1 2

[[2]]$`2`
[1] 3


[[3]]
[[3]]$`1`
[1] 1

[[3]]$`2`
[1] 2 3

Related


Get all combinations of elements from two lists?

List if i have two lists l1 = [ 'A', 'B' ] l2 = [ 1, 2 ] What is the most elegant way to get a pandas dataframe like this: +-----+-----+-----+ | | l1 | l2 | +-----+-----+-----+ | 0 | A | 1 | +-----+-----+-----+ | 1 | A | 2 | +-----+-----+--

Get all combinations of elements from two lists?

List if i have two lists l1 = [ 'A', 'B' ] l2 = [ 1, 2 ] What is the most elegant way to get a pandas dataframe like this: +-----+-----+-----+ | | l1 | l2 | +-----+-----+-----+ | 0 | A | 1 | +-----+-----+-----+ | 1 | A | 2 | +-----+-----+--

Get all combinations of elements from two lists?

List if i have two lists l1 = [ 'A', 'B' ] l2 = [ 1, 2 ] What is the most elegant way to get a pandas dataframe like this: +-----+-----+-----+ | | l1 | l2 | +-----+-----+-----+ | 0 | A | 1 | +-----+-----+-----+ | 1 | A | 2 | +-----+-----+--

Get all combinations of elements from two lists?

List if i have two lists l1 = [ 'A', 'B' ] l2 = [ 1, 2 ] What is the most elegant way to get a pandas dataframe like this: +-----+-----+-----+ | | l1 | l2 | +-----+-----+-----+ | 0 | A | 1 | +-----+-----+-----+ | 1 | A | 2 | +-----+-----+--

R - all pairwise combinations of elements in two lists

username I'm pretty sure there's an easy answer to this, but due to my limited R experience I'm having a hard time solving it. I have a list of dataframes representing different experiments, and for each of these dataframes I generate a regression model - the

R - all pairwise combinations of elements in two lists

username I'm pretty sure there's an easy answer to this, but due to my limited R experience I'm having a hard time solving it. I have a list of dataframes representing different experiments, and for each of these dataframes I generate a regression model - the

Find all combinations to split a single list into two lists

Arnold I have a list with few elements and I want to find all possibilities to split this list into two lists. I mean, all combinations mean I don't care about the order of its elements. That is, if elements 2 and 3 are in one list, then element 1 is in the ot

How to split a list into two unique lists in Python?

moemous Hi I have a list like this: listt = ['a','b','c','d','e','f','g','h','i','j' , 'k', 'l', 'm', 'n', 'o'] 15 members. I want to convert it to 3 lists, I used the code that works, but I want the only list. This gives me 3 lists with common members. import

How to split a list into two unique lists in Python?

moemous Hi I have a list like this: listt = ['a','b','c','d','e','f','g','h','i','j' , 'k', 'l', 'm', 'n', 'o'] 15 members. I want to convert it to 3 lists, I used the code that works, but I want the only list. This gives me 3 lists with common members. import

How to split a list into two unique lists in Python?

moemous Hi I have a list like this: listt = ['a','b','c','d','e','f','g','h','i','j' , 'k', 'l', 'm', 'n', 'o'] 15 members. I want to convert it to 3 lists, I used the code that works, but I want the only list. This gives me 3 lists with common members. import

Get all unique combinations from a permutation list

Alex TL / DR Given X = {(A,B),(B,C),(D,E),(B,A),(C,B)}( Xone of set) How to filter the sub-tuple which shows a unique combination (rather than a unique combination) so that it Xbecomes{(A,B),(B,C),(D,E))} longer form Most combination/permutation problems here

Get all unique combinations from a permutation list

Alex TL / DR Given X = {(A,B),(B,C),(D,E),(B,A),(C,B)}( Xone of set) How to filter the sub-tuple which shows a unique combination (rather than a unique combination) so that it Xbecomes{(A,B),(B,C),(D,E))} longer form Most combination/permutation problems here

Get all unique combinations from a permutation list

Alex TL / DR Given X = {(A,B),(B,C),(D,E),(B,A),(C,B)}( Xone of set) How to filter the sub-tuple which shows a unique combination (rather than a unique combination) so that it Xbecomes{(A,B),(B,C),(D,E))} longer form Most combination/permutation problems here

Get all combinations of two elements with distance 1

Peaceful Kegan Liberation I'm really sorry for the title, but I don't know how to describe my problem in words. So here is the example: Assuming we have a string "123", my function should yield: 1 2 3 12 3 1 23 or for the string "1234": 1 2 3 4 12 3 4 12 34 1

Get all combinations of two elements with distance 1

Peaceful Kegan Liberation I'm really sorry for the title, but I don't know how to describe my problem in words. So here is the example: Assuming we have a string "123", my function should yield: 1 2 3 12 3 1 23 or for the string "1234": 1 2 3 4 12 3 4 12 34 1

How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

Python: Get all combinations of sequential elements of a list

Minatverma Given an array say x = ['A','I','R']I want the output as [['A','I','R'],['A','I'],['I','R'],['A'],['I'],['R']] What I don't want as output is: [['A','I','R'],['A','I'],['I','R'],['A','R'],['A'],['I'],['R']] # extra ['A','R'] which is not in sequen

How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

Python: Get all combinations of sequential elements of a list

Minatverma Given an array say x = ['A','I','R']I want the output as [['A','I','R'],['A','I'],['I','R'],['A'],['I'],['R']] What I don't want as output is: [['A','I','R'],['A','I'],['I','R'],['A','R'],['A'],['I'],['R']] # extra ['A','R'] which is not in sequen

Get all combinations of neighbor elements in a list

Milano Is it possible to get all element combinations if they are neighbors? Here is an example: Edit: I want to use this on strings, not just numbers. E.g:[Explain,it,to,me,please] List: [0,1,2,3,4] result: [0,1,2,3,4], [0,1,2,3], [1,2,3,4], [0,1,2], [1,2,3]

How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

Python: Get all combinations of sequential elements of a list

Minatverma Given an array say x = ['A','I','R']I want the output as [['A','I','R'],['A','I'],['I','R'],['A'],['I'],['R']] What I don't want as output is: [['A','I','R'],['A','I'],['I','R'],['A','R'],['A'],['I'],['R']] # extra ['A','R'] which is not in sequen