Search code examples
rcombn

How would I find all combinations/permutations with order mattering in R?


Hello and thanks for looking. I'm not exactly sure how to ask this question - let me show you an example and what I'm hoping to return.

example <- c(1, 2, 3, 4, 5, 6)
example_combos <- combn(example, 4) 

1) How would I adapt combn() to just give me the combinations including the number 1? Additionally, I would like to not allow for sequences of three or more numbers to be in a row. i.e.: (1,2,4,5), (1,2,4,6), (1,2,5,6), (1,3,4,5), (1,3,4,6), (1,3,5,6)

2) Alternatively, how would I actually allow order to matter - meaning (1,2,4,5) is different from (2,4,5,1). How would I not allow for sequences of 3 or more in a row in this situation - and then, how would I subset the data frame to just include the columns starting with a 1?

Thank you! Very new to R still!


Solution

  • Here's the answer (or an answer anyway).

    example_combos[ , apply( example_combos, 2, function(col){ !any( rle(diff(col))$lengths >= 2 & rle(diff(col))$values==1)   })  ]
         [,1] [,2] [,3] [,4] [,5] [,6]
    [1,]    1    1    1    1    1    2
    [2,]    2    2    2    3    3    3
    [3,]    4    4    5    4    5    5
    [4,]    5    6    6    6    6    6
    

    And here's an explanation:

    I'm counting the number of time that a run of 1's in the successive differences of the combinations (which are ordered) occurs in any given column of the example_combos matrix. If its greater than or equal to two that implies a run of at least 3.