Home > OS >  Is it possible to fragment a vector into all different possible fragment combinations?
Is it possible to fragment a vector into all different possible fragment combinations?

Time:07-21

For example, I have a vector c(1,2,3,4,5)

There are different fragment combinations:

Combination 0: 1 2 3 4 5

Combination 1: 12345

Combination 2: 1234 5

Combination 3: 123 45

Combination 4: 12 345

Combination 5: 1 2345

Combination 6: 12 34 5

Combination 7: 12 3 45

Combination 8: 1 23 45

Combination 9: 15 23 4

Combination 10: 14 23 5

… and so on (there are a lot of combinations).

While the order does not matter: 14 23 5 is equivalent to 5 41 32 (Hence, only one of these combinations is needed).

Is there a way in R to fragment a vector into all different possible fragment combinations?

I would like the output to be a (obviously very long) nested list containing all different possible fragment combinations.

In case of combination 9 and 10:

list(
list(c(1,5), c(2,3), 4),
list(c(1,4), c(2,3), 5)
)

[[1]]
[[1]][[1]]
[1] 1 5

[[1]][[2]]
[1] 2 3

[[1]][[3]]
[1] 4


[[2]]
[[2]][[1]]
[1] 1 4

[[2]][[2]]
[1] 2 3

[[2]][[3]]
[1] 5

CodePudding user response:

I think akrun has the correct answer. Just a little typo:

library(partitions)
v1 <- 1:5
lapply(listParts(length(v1)), unclass)
  •  Tags:  
  • r
  • Related