I am trying to get all possible splitting sequences [1:n]
in R. For example:
getSplits(0,3)
Should return all possible splitting sequences 123, in other words (in the list of vectors):
[1] 1
[2] 1 2
[3] 1 2 3
[4] 1 3
[5] 2
[6] 2 3
[7] 3
Now I created a function that recursively transitioned to these vectors, but could not combine them into one, as indicated above. My function:
getSplits <- function(currentDigit, lastDigit, split) {
splits=list();
for (nextDigit in currentDigit: lastDigit)
{
currentSplit <- c(split, c(nextDigit));
print(currentSplit);
if(nextDigit < lastDigit) {
possibleSplits = c(list(currentSplit), getSplits(nextDigit+1, lastDigit, currentSplit));
}else{
possibleSplits = currentSplit;
}
splits <- c(splits, list(possibleSplits));
}
return(splits);
}
If printing each currentSplit leads to all the vectors I need, but somehow the final list of returnt (splits) inserts them into deeper levels of the lists, returning:
[1] 1
[[1]][[2]]
[[1]][[2]][[1]]
[1] 1 2
[[1]][[2]][[2]]
[1] 1 2 3
[[1]][[3]]
[1] 1 3
[[2]]
[[2]][[1]]
[1] 2
[[2]][[2]]
[1] 2 3
[[3]]
[1] 3
To call the corresponding function getSplits(1, 3, c())
.
If someone can help me get this to work as I described above, it would be very grateful!