Dividing a sequence of unknown length into a number of sets from R

I have a sequence that varies in length, for example. sort of:

items <- 1:4

I want to break it down into all sorts of combinations of the nnumber of sets. So to speak n- two, I want to return:

Set A    Set B
-----    -----
1        2 3 4
1 2      3 4
1 2 3    4
1 3      2 4 

etc .. Location within sets does not matter, i.e. the set { 1, 2, 3} is { 2, 1, 3}. Sets cannot be empty.

Best I could come up with (using permnfrom the package combinat):

n <- 2
r <- 1:length(items)
arrangements <- NULL
for (i in 1:(n-1)) {
  A <- r[(1:i)]
  B <- r[-(1:i)]
  arrangements <- c(arrangements, apply(do.call(rbind, permn(1:length(items))), 1, function(z) list(z[A], z[B])))
}

, , , .. { 1, 2, 3} {2, 1, 3} n. - , ? .

+5
2

'sets':

require(sets)
power_set(1:4)
sapply( set_power(1:4) , function(x) set_complement(x ,as.set(1:4)) )
list( Set_A = as.list(set_power(1:4)), 
      Set_B = sapply( set_power(1:4) , function(x) set_complement(x ,as.set(1:4)) ) )

, ({1,2,3,4}, {}), , "". ( N, Set_B.)

+5

, :

# Params
n <- 2
items <- 1:4

# Sample
l <- lapply(items, function(x) combn(items, x, simplify=F))
l <-unlist(l, recursive=F)

# devide into sets
tmp <- 1:length(l)
tmp <- split(tmp, sample(1:n, length(l), replace=T))

sets <- lapply(tmp, function(x) l[x])
+1

All Articles