gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:26:51 26 4
gpt4 key购买 nike

对于这个例子,我将使用 data.table包裹。

假设你有一张教练 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

和一 table 玩家
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对于没有球员选择的任何教练的值(value),您可以执行以下操作:
    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

    关于r - 如何在两个 data.tables(或 data.frames)的行之间创建随机匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30086641/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com