R : 随机抽样将学生分配到相等的组.理解 rep() 参数 length.out 到 sample() [英] R : Assigning students to equal groups with random sampling. Understanding rep() argument length.out to sample()
问题描述
我有 33 名学生,我想在 5 种不同的场合将他们分成 6 人一组(或尽可能接近).所以我给不同场合的学生分配一个 1 到 6 之间的数字.
I have 33 students I want to sort into groups of 6 (or as close as possible) on 5 different occasions. So I assign a number between 1 and 6 to the students on different occassions.
我管理了以下内容:
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 之间的随机样本.
This seems to work. As I've understood, I ask for each row a random sample between 1 and 6.
来自 rep() 的 length.out 参数如何与 sample() 交互?
当我运行频率表来检查组的大小时,我发现以下内容:
When I run a frequency table to check the sizes of the groups, I find the following:
numb=1,2,3,4,5,6.大小=6,6,6,5,5,5.
numb=1,2,3,4,5,6. size=6,6,6,5,5,5.
我尝试改为要求 7 个组,但得到的组大小为:
I tried asking for 7 groups instead, and got group sizes of:
numb=1,2,3,4,5,6,7.大小=5,5,5,5,5,4,4.
numb=1,2,3,4,5,6,7. size=5,5,5,5,5,4,4.
为什么他们以这种递减的方式组织起来?
Why are they organised in this decreasing fashion?
推荐答案
之所以有这种特定模式,是因为 rep 函数与 length.out 一起工作的方式.如果要创建 6 人一组,
You have this specific pattern because of how the rep function works with length.out. If you want to create groups of 6,
rep(1:6, length.out = 33)
将首先将数字 1 到 6 重复 5 次(生成 30 个值),并用值 1、2 和 3 补全缺失的 3 个.因此,您将始终在第 1、2 和 3 组中多出一名学生.
will first repeat the numbers 1 to 6 5 times (generating 30 values) and complete the 3 missing ones with values 1, 2 and 3. So you will always have one more student in the groups 1, 2 and 3.
这篇关于R : 随机抽样将学生分配到相等的组.理解 rep() 参数 length.out 到 sample()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!