How to find all the possible samples of 3 from a population of 5 in Rstudio?

151 Views Asked by At

I'm trying to find all the possible samples of 3 of this population in Rstudio: 12, 12, 14, 15, 20.

I know by using the combination formula that it will be 10 of them. I want to find all the list using Rstudio. I tried to do it manually but I just get 9 of them:

  • 12,12,14
  • 12,12,15
  • 12,12,20
  • 12,14,15
  • 12,14,20
  • 12,15,20
  • 12,14,15
  • 12,15,20
  • 14,15,20

Will appreciate your help.

3

There are 3 best solutions below

0
On BEST ANSWER

When you say "the combination formula", you presumably mean ${5 \choose 3}=10$. This suggests you are treating the two $12$s as completely distinct, as otherwise there might only be seven possibilities.

This is also an issue with some R functions but is not difficult to work round by introducing a distinction and then removing it. For example using combinations() from the gtools package, you could try something like:

library(gtools)
floor(combinations(5, 3, c(12, 12.1, 14, 15, 20))) 

to get

      [,1] [,2] [,3]
 [1,]   12   12   14
 [2,]   12   12   15
 [3,]   12   12   20
 [4,]   12   14   15
 [5,]   12   14   20
 [6,]   12   15   20
 [7,]   12   14   15
 [8,]   12   14   20
 [9,]   12   15   20
[10,]   14   15   20

and, as Quinlan Uyeda has pointed out, your error was to miss the 8th row

1
On

You're missing 12 14 20, it should be 8th in your list. The first six start with the first 12, then the next 3 start with the second twelve.

0
On

In R,

combn(c(12,12,14,15,20), 3)

yields

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]   12   12   12   12   12   12   12   12   12    14
[2,]   12   12   12   14   14   15   14   14   15    15
[3,]   14   15   20   15   20   20   15   20   20    20

The results are in the columns.