随机化R排序

时间:2017-06-20 07:08:02

标签: r random

我恐怕无法找到问题的答案。

我正在寻找

1)它们是4组卡A,B,C,D和16卡。 2)每张卡在一组中编号(A从1到4,B从5到8,依此类推)。 3)我们希望随机化分配,以便为每个人随机分配一组卡片,例如A. 4)此外,集合中的卡的顺序必须是随机的。

所以我们想要的是以下内容: 人1:设置A,卡1-2-3-4 人2:设置A,卡4-2-3-1 人3:设置D,卡16-15-12-13 等等。

我还希望每个数字都在一个单独的列中。

感谢您的帮助! S上。

3 个答案:

答案 0 :(得分:1)

如果每个人都获得一套卡

 > 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

答案 1 :(得分:0)

这是解决这个问题的一种方法。

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

答案 2 :(得分:0)

这是另一种选择:

# 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))

}