在R中创建多个对

时间:2013-03-22 14:50:31

标签: r match random-sample

我有一份人员名单:

people<-c("Betty", "Joe", "Bob", "Will", "Frank")

我想随机分配两个人给每个人(不允许自我分配),我希望每个人被分配给另一个人相同的次数(在上面的例子中,每个人只能分配到另外两次)。

例如,匹配可能是

贝蒂(乔和鲍勃),乔(鲍勃和威尔),鲍勃(威尔和弗兰克),威尔(弗兰克和贝蒂)

当然我刚刚使用了他们的订购,但如果这可以随机化会很好。

任何想法从哪里开始?

2 个答案:

答案 0 :(得分:3)

新的(更简单的)解决方案:使用来自Ari B. Friedman的shift软件包中的TaRifx函数

tt <- sample(people)
lapply(seq_len(length(tt))-1, function(x) shift(tt, x)[1:3])
# if you don't want it to be ordered, just add a sample(.)
lapply(seq_len(length(tt))-1, function(x) sample(shift(tt, x)[1:3]))
# [[1]]
# [1] "Bob"   "Frank" "Betty"
# 
# [[2]]
# [1] "Frank" "Betty" "Joe"  
# 
# [[3]]
# [1] "Betty" "Joe"   "Will" 
# 
# [[4]]
# [1] "Joe"  "Will" "Bob" 
# 
# [[5]]
# [1] "Will"  "Bob"   "Frank"

旧解决方案(对于这个想法): 我会走这条路。基本上,一旦你sample“人”,你可以总是去,1,2,3,2,3,4,3,4,5,4,5,1。所以,让我们这样做。也就是说,生成这些索引,然后对人进行抽样并得到三元组。

# generate index
len <- length(people)
choose <- 3 # at a time 
idx <- outer(seq(choose), seq(choose+2)-1, '+')
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    1    2    3    4    5
# [2,]    2    3    4    5    6
# [3,]    3    4    5    6    7

# sample people
tt <- sample(people)
# [1] "Joe"   "Will"  "Bob"   "Frank" "Betty"
max.idx <- 2*choose + 1
tt[(len+1):max.idx] <- tt[seq(max.idx-len)]
# [1] "Joe"   "Will"  "Bob"   "Frank" "Betty" "Joe"   "Will" 

tt[idx]
#  [1] "Joe"   "Will"  "Bob"   "Will"  "Bob"   "Frank" "Bob"   "Frank" "Betty" "Frank" 
#  [15] "Betty" "Joe"  "Betty" "Joe"   "Will" 

split(tt[idx], gl(ncol(idx), nrow(idx)))
# $`1`
# [1] "Joe"  "Will" "Bob" 
# 
# $`2`
# [1] "Will"  "Bob"   "Frank"
# 
# $`3`
# [1] "Bob"   "Frank" "Betty"
# 
# $`4`
# [1] "Frank" "Betty" "Joe"  
# 
# $`5`
# [1] "Betty" "Joe"   "Will" 

现在我们可以把这一切都放在一个函数中:

my_sampler <- function(x, choose) {
    len <- length(x)
    idx <- outer(seq(choose), seq(choose+2)-1, '+')
    sx  <- sample(x)
    max.idx <- 2*choose + 1
    sx[(len+1):max.idx] <- sx[seq(max.idx-len)]
    split(sx[idx], gl(ncol(idx), nrow(idx)))
}
# try it out
my_sampler(people, 3)
my_sampler(people, 4) # 4 at a time

# if you want this and want a non-ordered solution, wrap this with `lapply` and `sample`

lapply(my_sampler(people, 3), sample)

答案 1 :(得分:0)

有趣的问题。这将让你在那里中途。缺少的是对同等数量伙伴关系的人的限制。如果你想随机地将两个人分配给除了他们自己以外的人,这可以通过像这样的一个班轮来实现......

assigns <- lapply( people , function(x) { c( x , sample( people[!(people %in% x)] , 2 ) ) } )

第一人将是受让人,最后两人将被分配。

assigns
#[[1]]
#[1] "Betty" "Bob"   "Will" 

#[[2]]
#[1] "Joe"   "Bob"   "Frank"

#[[3]]
#[1] "Bob"   "Betty" "Joe"  

#[[4]]
#[1] "Will"  "Betty" "Joe"  

#[[5]]
#[1] "Frank" "Will"  "Betty"