2

I have a set:

lynx <- c(1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,4,4,4,5,5,6,7,8,9)

I want to return a set of all combinations with repetitions allowed from the above set, for example:

1 1 1 1 1
1 2 2 8 9

I used the combination function from library gtools but it doesn't help

I tried:

combination(n = 9, r = 5, v =  lynx, repeats.allowed=TRUE)

which returned

        [,1] [,2] [,3] [,4] [,5]
    [1,]    1    1    1    1    1
    [2,]    1    1    1    1    2
    [3,]    1    1    1    1    3
    [4,]    1    1    1    1    4

But the issue is that it also returns,

  [152,]    1    1    2    8    8

which I don't want since there are no two 8s in the set.

azmath
  • 97
  • 6
  • I suspect you want `permutations(n = length(lynx), r = 5, v = lynx, set = FALSE)`. – Roland Dec 09 '20 at 14:37
  • No, that wouldn't work – azmath Dec 09 '20 at 14:46
  • @Roland won't that produce multiple copies of some indistinguishable vectors? For example, there would be 154,440 rows consisting solely of 1s. It looks like the OP wants such identical rows collapsed to a single instance. It would also output 69 million rows if it completed – Allan Cameron Dec 09 '20 at 14:47
  • @azmath the result of this calculation is so vast, it does raise the question of why you would need to precalculate all such instances. Why not sample as required? If you just want to calculate the number of permutations there are mathematical shortcuts. – Allan Cameron Dec 09 '20 at 14:53
  • @AllanCameron Calculation took about two minutes on my system. Removing duplicates is easy. – Roland Dec 09 '20 at 15:08
  • @Roland my cloud based server gave up after about two minutes, though to be fair it only has 8 GB of RAM. I'm sure you managed it on a decent computer (I'm guessing the resultant vector should be about 1.4 GB) - but difficult to see the purpose without more info from the OP. – Allan Cameron Dec 09 '20 at 15:47
  • @AllanCameron According to RStudio, the result is a matrix of 2.8 GB. R's RAM usage went up to 6.3 GB (it isn't freed by the garbage collector immediately). I have 32 GB of RAM. – Roland Dec 09 '20 at 16:19
  • @AllanCameron What Roland gave is not what Im looking for. It should not be that large a set – azmath Dec 09 '20 at 17:02
  • @azmath no, but you can remove duplicates easily enough from it. Are you sure you need the whole set? Can I ask what your use case is? – Allan Cameron Dec 09 '20 at 17:39
  • 2
    @azmath It seems that you are looking for a way to generate combinations/permutations without replacement but with non-distinct items (a.k.a [multisets](https://en.wikipedia.org/wiki/Multiset)). Please read this [post](https://stackoverflow.com/a/22569328/10802499), which did a fairly straightforward walk-through of 8 basic combinatoric problems, including the one you have encountered. – ekoam Dec 09 '20 at 18:07
  • One way is to create a matching vector `lyncidx <- 1:length(lynx)` , do standard combination or permutation on that set, and then use the results to index into `lynx` – Carl Witthoft Dec 09 '20 at 23:05

2 Answers2

0

One way is to create a matching vector lyncidx <- 1:length(lynx) , do standard combination or permutation on that set, and then use the results to index into lynx .
lyncidx contains all distinguishable items, so standard set theory tools work fine. Then, e.g., for an output of foo = 1,7,12,20 you would grab the values of lynx[foo] to get

1,1,1,2 as desired

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • Hu Carl, what do you mean by indexing lynncidx to lynx? – azmath Dec 11 '20 at 00:06
  • @azmath Use the values in `lyncidx` to create your collection of combinations. Then for each combination created, use that vector to select the elements of `lynx` for your final "actual" combinations of `lynx` elements. – Carl Witthoft Dec 11 '20 at 00:42
0

Thanks @ekoam

lynx <- c(1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,4,4,4,5,5,6,7,8,9)

comb <- combinations(freq = table(lynx), k = 5, x = unique(lynx))

comb

library: arrangements

azmath
  • 97
  • 6