Skip to content

Create unique sets of 3 from a vector of IDs?

6 messages · philozine, Dylan Beaudette, Kingsford Jones +1 more

#
Dear all:

This is one of those "should be easy" problems that I'm having great difficulty solving. I have a vector containing ID codes, and I need to generate a 3-column matrix that contains all possible combinations of three.

For example, my ID vector looks like this:
A
B
C
D
E

I need to generate a matrix that looks like this:
A B C
A B D
A B E
A C B
A C D
A C E
A D B
A D C
A D E
.... and so on. (Yes, this could yield some very large matrices!) It's totally fine, though less desirable, if the resulting matrix contains combinations with "duplicates," e.g., A A A, A A B, or whatever.

Thoughts? I'm pretty much stumped here. Thanks in advance for any help you can offer.

Best regards,

Brandon
#
On Tue, Dec 2, 2008 at 7:42 PM, philozine <philozine at yahoo.com> wrote:
Hi,

Does this do what you want?

expand.grid(letters[1:5], letters[1:5], letters[1:5])


D
#
Dear Brandon,

On Tue, Dec 2, 2008 at 10:46 PM, Dylan Beaudette
<dylan.beaudette at gmail.com> wrote:
Have a look at urnsamples() in the prob package.

ID <- LETTERS[1:5]
urnsamples(ID, size = 3, replace = FALSE, ordered = FALSE)

Best,
Jay




***************************************************
G. Jay Kerns, Ph.D.
Associate Professor
Department of Mathematics & Statistics
Youngstown State University
Youngstown, OH 44555-0002 USA
Office: 1035 Cushwa Hall
Phone: (330) 941-3310 Office (voice mail)
-3302 Department
-3170 FAX
E-mail: gkerns at ysu.edu
http://www.cc.ysu.edu/~gjkerns/
#
However, I believe Brandon was trying to get the permutations of size
3, rather than combinations.  Dylan provided a solution including
repeats.  Here's one without:
[,1] [,2] [,3]
 [1,] "A"  "B"  "C"
 [2,] "A"  "B"  "D"
 [3,] "A"  "B"  "E"
 [4,] "A"  "C"  "B"
 [5,] "A"  "C"  "D"
 [6,] "A"  "C"  "E"
 [7,] "A"  "D"  "B"
 [8,] "A"  "D"  "C"
 [9,] "A"  "D"  "E"
[10,] "A"  "E"  "B"
[11,] "A"  "E"  "C"
[12,] "A"  "E"  "D"
[13,] "B"  "A"  "C"
[14,] "B"  "A"  "D"
[15,] "B"  "A"  "E"
[16,] "B"  "C"  "A"
[17,] "B"  "C"  "D"
[18,] "B"  "C"  "E"
[19,] "B"  "D"  "A"
[20,] "B"  "D"  "C"
[21,] "B"  "D"  "E"
[22,] "B"  "E"  "A"
[23,] "B"  "E"  "C"
[24,] "B"  "E"  "D"
[25,] "C"  "A"  "B"
[26,] "C"  "A"  "D"
[27,] "C"  "A"  "E"
[28,] "C"  "B"  "A"
[29,] "C"  "B"  "D"
[30,] "C"  "B"  "E"
[31,] "C"  "D"  "A"
[32,] "C"  "D"  "B"
[33,] "C"  "D"  "E"
[34,] "C"  "E"  "A"
[35,] "C"  "E"  "B"
[36,] "C"  "E"  "D"
[37,] "D"  "A"  "B"
[38,] "D"  "A"  "C"
[39,] "D"  "A"  "E"
[40,] "D"  "B"  "A"
[41,] "D"  "B"  "C"
[42,] "D"  "B"  "E"
[43,] "D"  "C"  "A"
[44,] "D"  "C"  "B"
[45,] "D"  "C"  "E"
[46,] "D"  "E"  "A"
[47,] "D"  "E"  "B"
[48,] "D"  "E"  "C"
[49,] "E"  "A"  "B"
[50,] "E"  "A"  "C"
[51,] "E"  "A"  "D"
[52,] "E"  "B"  "A"
[53,] "E"  "B"  "C"
[54,] "E"  "B"  "D"
[55,] "E"  "C"  "A"
[56,] "E"  "C"  "B"
[57,] "E"  "C"  "D"
[58,] "E"  "D"  "A"
[59,] "E"  "D"  "B"
[60,] "E"  "D"  "C"


Kingsford Jones
On Tue, Dec 2, 2008 at 9:41 PM, G. Jay Kerns <gkerns at ysu.edu> wrote:
#
Dear Kingsford,

You are quite right, my mistake:

urnsamples(ID, size = 3, replace = FALSE, ordered = TRUE)

Thanks.
Jay


On Wed, Dec 3, 2008 at 12:04 AM, Kingsford Jones
<kingsfordjones at gmail.com> wrote:

  
    
#
Dear all:

These solutions work beautifully. Many, many thanks. And apologies for my lazy language! I did indeed mean permutations....

Best regards,

Brandon
--- On Wed, 12/3/08, G. Jay Kerns <gkerns at ysu.edu> wrote: