繁体   English   中英

如何在两个data.tables(或data.frames)的行之间创建随机匹配

[英]How to create a random matching between the rows of two data.tables (or data.frames)

对于此示例,我将使用data.table包。

假设你有一张教练桌

coaches <- data.table(CoachID=c(1,2,3), CoachName=c("Bob","Sue","John"), NumPlayers=c(2,3,0))
coaches
   CoachID CoachName NumPlayers
1:       1       Bob          2
2:       2       Sue          3
3:       3      John          0

还有一张球员表

players <- data.table(PlayerID=c(1,2,3,4,5,6), PlayerName=c("Abe","Bart","Chad","Dalton","Egor","Frank"))
players
   PlayerID PlayerName
1:        1        Abe
2:        2       Bart
3:        3       Chad
4:        4     Dalton
5:        5       Egor
6:        6      Frank

你想让每个教练与一组球员相匹配

  • 与每个教练相关联的球员数量由NumPlayers字段定义
  • 没有两个教练被绑在同一个球员身上
  • 球员和教练是随机匹配的

你好吗?

exampleResult <- data.table(CoachID=c(1,1,2,2,2,3), PlayerID=c(3,1,2,5,6,NA))
exampleResult

   CoachID PlayerID
1:       1        3
2:       1        1
3:       2        2
4:       2        5
5:       2        6
6:       3       NA

你可以在没有替换玩家ID的情况下进行采样,获取你需要的玩家总数:

set.seed(144)
(selections <- sample(players$PlayerID, sum(coaches$NumPlayers)))
# [1] 1 4 3 2 6

每个玩家将有相同的概率被包括在selections ,并且该向量的排序是随机的。 因此,您可以将这些球员分配到每个教练位置:

data.frame(CoachID=rep(coaches$CoachID, coaches$NumPlayers),
           PlayerID=selections)
#   CoachID PlayerID
# 1       1        1
# 2       1        4
# 3       2        3
# 4       2        2
# 5       2        6

如果您希望任何没有选择球员的教练拥有NA值,您可以执行以下操作:

rbind(data.frame(CoachID=rep(coaches$CoachID, coaches$NumPlayers),
                 PlayerID=selections),
      data.frame(CoachID=coaches$CoachID[coaches$NumPlayers==0],
                 PlayerID=rep(NA, sum(coaches$NumPlayers==0))))
#   CoachID PlayerID
# 1       1        1
# 2       1        4
# 3       2        3
# 4       2        2
# 5       2        6
# 6       3       NA

获取每一方的需求和供应,可以这么说:

demand <- with(coaches,rep(CoachID,NumPlayers))
supply <- players$PlayerID

然后我会......

randmatch <- function(demand,supply){
  n_demand  <- length(demand)
  n_supply  <- length(supply)
  n_matches <- min(n_demand,n_supply)

  if (n_demand >= n_supply) 
    data.frame(d=sample(demand,n_matches),s=supply)
  else 
    data.frame(d=demand,s=sample(supply,n_matches))
}

例子:

set.seed(1)
randmatch(demand,supply)    # some players unmatched, OP's example
randmatch(rep(1:3,1:3),1:4) # some coaches unmatched 

不过,我不确定这是OP想要覆盖的情况。


对于OP的期望输出......

m <- randmatch(demand,supply)
merge(m,coaches,by.x="d",by.y="CoachID",all=TRUE)
#   d  s CoachName NumPlayers
# 1 1  2       Bob          2
# 2 1  6       Bob          2
# 3 2  3       Sue          3
# 4 2  4       Sue          3
# 5 2  1       Sue          3
# 6 3 NA      John          0

同样...

merge(m,players,by.x="s",by.y="PlayerID",all=TRUE)
#   s  d PlayerName
# 1 1  2        Abe
# 2 2  1       Bart
# 3 3  2       Chad
# 4 4  2     Dalton
# 5 5 NA       Egor
# 6 6  1      Frank

这是使用简单的dplyr的答案。 首先选择教练需要,然后对球员的需求进行抽样,最后将其全部结合起来。

library(dplyr)

set.seed(1234)

coach_needs <- coaches %>%
  group_by( CoachID ) %>%
  do( sample_n(., size=.$NumPlayers, replace=TRUE) ) %>%
  select( -CoachID ) %>% ungroup()

player_needs <- players %>%
  sample_n( size = nrow(coach_needs))

result <- cbind(coach_needs, player_needs)

result

这给了我:

   CoachID CoachName NumPlayers PlayerID PlayerName
1:       1       Bob          2        4     Dalton
2:       1       Bob          2        1        Abe
3:       2       Sue          3        5       Egor
4:       2       Sue          3        2       Bart
5:       2       Sue          3        3       Chad

更新:如果NumPlayer == 0教练需要NA s,那么这是一个简单的NumPlayer == 0

result <- cbind(coach_needs, player_needs) %>%
  rbind( coaches %>% filter(NumPlayers == 0), fill=TRUE )

result

这给了我这个:

   CoachID CoachName NumPlayers PlayerID PlayerName
1:       1       Bob          2        4     Dalton
2:       1       Bob          2        1        Abe
3:       2       Sue          3        5       Egor
4:       2       Sue          3        2       Bart
5:       2       Sue          3        3       Chad
6:       3      John          0       NA         NA

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM