按 R 中选定的组随机化样本

发布于 2025-01-13 09:44:20 字数 855 浏览 2 评论 0原文

在我的数据集中,90 个人(样本)必须每人玩两种类型的游戏,总共四种类型:XYZW。我想在 R 中随机化每个人将玩哪些游戏以及游戏顺序,以便遵循以下方式:

UA第一场比赛第二场比赛
人员 1游戏 X游戏 W
人员 2游戏 W游戏 Y
人员 3游戏 Z游戏W

因此,每个游戏必须由相同数量的人玩(,每场游戏 30 人),并且游戏 W 必须由每个人玩。在 R 中是否有一种简单的方法可以做到这一点?

In my dataset, 90 people (samples) must each play two types of game, of a total of four types: X, Y, Z and W. I would like to randomize in R which games each person will play, as well as the game order, so that it follows this way:

UAFirst gameSecond game
Person 1Game XGame W
Person 2Game WGame Y
Person 3Game ZGame W

Thus, each game must be played by the same number of people (i.e., 30 people per game), and game W must be played by everyone. Is there a simple way to do this in R?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怼怹恏 2025-01-20 09:44:20
  1. 创建包含 30 次游戏 X、Y、Z 代表的向量
  2. 创建该向量
  3. 到每个游戏的随机排列 添加游戏 W
  4. 排列每个游戏对
games <- t(
  apply(
    data.frame(
      first_game = sample(rep(c("X", "Y", "Z"), 30), 90),
      second_game = "W"
    ),
    1,
    function(x) sample(x, 2)
  )
)

games <- cbind(paste("pearson", 1:90), games)
colnames(games) <- c("UA" ,"first_game", "second_game")
games <- as.data.frame(games)
  1. create vector that contains 30 reps of games X, Y, Z
  2. create random permutation of that vector
  3. to each game add game W
  4. permute each game pair
games <- t(
  apply(
    data.frame(
      first_game = sample(rep(c("X", "Y", "Z"), 30), 90),
      second_game = "W"
    ),
    1,
    function(x) sample(x, 2)
  )
)

games <- cbind(paste("pearson", 1:90), games)
colnames(games) <- c("UA" ,"first_game", "second_game")
games <- as.data.frame(games)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文