Daniel Rawlings
Daniel Rawlings

Reputation: 183

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

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

x <- c(1, 2, 3)

and want to split them into two lists (using all entries) but I want to get all possible combinations of that list to be returned e.g.:

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

Does anyone know an efficient way to do this for a more complex list?

Thanks in advance for your help!

Upvotes: 1

Views: 255

Answers (1)

Karolis Koncevičius
Karolis Koncevičius

Reputation: 9656

List with 3 elements:

vec <- 1:3

Note that for each element we have two possibilities: it is either in 1st split or in 2nd split. So we define a matrix of all possible splits (in rows) using expand.grid which produces all possible combinations:

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

However This will treat scenarios where the groups are flipped as different splits. Also will include scenarios where all the observations are in the same group (but there will only be 2 of them).

If you want to remove them we need to remove the lines from groups matrix that only have one group (2 such lines) and all the lines that split the vector in the same way, only switching the groups.

One-group entries are on top and bottom so removing them is easy:

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

Duplicated entries are a bit trickier. But note that we can get rid fo them by removing all the rows where the first group is 2. In effect this will make a requirement that the first element is always assigned to group 1.

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

Then the job is to split the list we have using each of the rows in the groups matrix. For that we use Map to call split() function on our list vec and each row of groups matrix:

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

Upvotes: 1

Related Questions