S. Solange
S. Solange

Reputation: 11

Randomization R ordering

I'm afraid I can't find an answer to my problem.

I am looking to create

1) They are 4 sets of cards A, B, C, D and 16 cards. 2) Each card is numbered within a set (A from 1 to 4, B from 5 to 8, and so on). 3) We want to randomize the assignment such that each person is randomly assigned a set of cards, for example A. 4) In addition, the order of the cards within the set has to be randomized.

So what we want is the following: Person 1: Set A, cards 1-2-3-4 Person 2: Set A, cards 4-2-3-1 Person 3: Set D, cards 16-15-12-13 and so on.

I would also like each number to be in a separate column.

Thanks for your help! S.

Upvotes: 1

Views: 51

Answers (3)

Ajay Ohri
Ajay Ohri

Reputation: 3492

if each person gets one set of cards

 > df=NULL
    > a=rep(LETTERS[1:4],4)
    > df$card1=sample(a,16,F)
    > df=as.data.frame(df)

> df=df[order(card1),]
> df
    card1 
 1:     A     
 2:     A     
 3:     A     
 4:     A     
 5:     B     
 6:     B     
 7:     B     
 8:     B     
 9:     C     
10:     C     
11:     C     
12:     C     
13:     D     
14:     D     
15:     D     
16:     D     
> df$card2=rep((1:4),4)
> df
    card1 card2
 1:     A     1
 2:     A     2
 3:     A     3
 4:     A     4
 5:     B     1
 6:     B     2
 7:     B     3
 8:     B     4
 9:     C     1
10:     C     2
11:     C     3
12:     C     4
13:     D     1
14:     D     2
15:     D     3
16:     D     4
> df1=df[sample(nrow(df)),]
> df1
    card1 card2
 1:     A     2
 2:     D     4
 3:     C     3
 4:     D     3
 5:     B     3
 6:     D     1
 7:     C     2
 8:     A     3
 9:     B     2
10:     D     2
11:     B     1
12:     A     1
13:     C     4
14:     C     1
15:     B     4
16:     A     4

Upvotes: 1

Hallie Swan
Hallie Swan

Reputation: 2764

Here's another option:

# create data frame of decks and their numbered cards
cards <- data.frame(deck = rep(LETTERS[1:4], each = 4),
                    numbers = c(1:16), 
                    stringsAsFactors = FALSE)
# create list of people
people <- c("Person1", "Person2", "Person3")

# loop through each person and randomly select a deck
# based on deck selected, subset the cards that can be used
# randomize the numbered cards
# add the deck, order of cards, and person to a 
# growing data frame of assignments

assignment <- NULL
for(i in unique(people)) {
  set <- sample(cards$deck, size = 1)
  setCards <- cards[cards$deck == set, ]
  orderCards <- sample(setCards$numbers)
  assignment <- rbind(assignment, data.frame(Person = i,
                                             Deck = set,
                                             Card1 = orderCards[1],
                                             Card2 = orderCards[2],
                                             Card3 = orderCards[3],
                                             Card4 = orderCards[4],
                                             stringsAsFactors = FALSE))

}

Upvotes: 0

Roman Luštrik
Roman Luštrik

Reputation: 70643

Here's one way of approaching this.

person <- c("Person1", "Person2", "Person3", "Person4")
cardset <- LETTERS[1:4]

set.seed(357) # this is for reproducibility

xy <- data.frame(
  person = sample(person), # pick out persons in a random order
  set = sample(cardset)) # assign a random card set to a person 

vx <- rep(xy$set, each = 4) # for each set, create repeats
vy <- split(paste(vx, rep(1:4, times = 4), sep = ""), f = vx) # append numbers to it
vz <- do.call(rbind, sapply(vy, FUN = sample, simplify = FALSE)) # shuffle using sapply and stitch together with do.call
cbind(xy, vz) # add it to the original data

   person set  1  2  3  4
A Person1   C A4 A3 A2 A1
B Person4   B B2 B1 B4 B3
C Person3   D C2 C3 C4 C1
D Person2   A D1 D2 D4 D3

Upvotes: 0

Related Questions