【问题标题】:R : Assigning students to equal groups with random sampling. Understanding rep() argument length.out to sample()R:随机抽样将学生分配到相等的组。了解 rep() 参数 length.out 到 sample()
【发布时间】:2019-11-01 14:28:10
【问题描述】:

我有 33 名学生,我想在 5 个不同的场合将他们分成 6 人一组(或尽可能接近)。所以我给不同场合的学生分配了一个 1 到 6 之间的数字。

我已经完成了以下工作:

studentlist <- data.frame(seq(1:33))

studentlist$Occassion1 <- sample(factor(rep(1:6, length.out=nrow(studentlist)), 
                                 labels=paste0(1:6)))
studentlist$Occassion2 <- sample(factor(rep(1:6, length.out=nrow(studentlist)), 
                                 labels=paste0(1:6)))
studentlist$Occassion3 <- sample(factor(rep(1:6, length.out=nrow(studentlist)), 
                                 labels=paste0(1:6)))
studentlist$Occassion4 <- sample(factor(rep(1:6, length.out=nrow(studentlist)), 
                                 labels=paste0(1:6)))
studentlist$Occassion5 <- sample(factor(rep(1:6, length.out=nrow(studentlist)), 
                                 labels=paste0(1:6)))

这似乎有效。 据我了解,我要求每行随机抽取 1 到 6 个样本。

rep() 中的 length.out 参数如何与 sample() 交互?

当我运行频率表来检查组的大小时, 我发现以下内容:

麻木=1,2,3,4,5,6。 大小=6,6,6,5,5,5。

我尝试改为要求 7 个组,但组大小为:

麻木=1,2,3,4,5,6,7。 大小=5,5,5,5,5,4,4。

为什么它们以这种递减的方式组织起来?

【问题讨论】:

    标签: r random sample rep


    【解决方案1】:

    由于 rep 函数如何与 length.out 一起工作,您具有这种特定模式。 如果你想创建 6 人一组,

    rep(1:6, length.out = 33) 
    

    首先将数字 1 到 6 重复 5 次(生成 30 个值),并用值 1、2 和 3 补全缺失的 3 个。 因此,第 1、第 2 和第 3 组中总会多一名学生。

    【讨论】:

      猜你喜欢
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多