将观察值均匀分成组的 R 函数
R function that evenly splits observations into groups
我有一个 30 x 2 数据框 (df),其中一列包含 30 个人的姓名,第二列包含他们的 ID#。
我想在 R 中创建一个函数,将 30 个人随机且最均匀地分成几组,并且可以处理有余数和无余数的除法。
澄清一下,这个函数将:
• 取2个参数作为参数:df和一个代表组数的整数
• 将原始 df 还给我,但多了一个列,其中包含每个人随机分配到的组号
• 如果人数(行数)不能除以给定的整数,则剩余的行数应尽可能均匀地分配给各组
例如:
• 如果我想将 30 人分成一组,我的函数应该 return df 有一个新列 "group_no",每个人都有 1 个(每个人将被分配到同一组)
• 如果我想要 4 个组,我希望看到 10 个人被分配到 2 个组,其余 5 个人被分配到另外 2 个组。
• 如果我想要 8 组,那么函数应该给我 6 组 4 人和 2 组 3 等等。
我已经编写了一些代码来满足我的需要,但我只是手动输入组,所以不只是它的随机性或正确性......我想改为将所有这些写在一个函数中可以自动执行这些任务:
#My code so far
#For 1 group of 30 people
people=1:30
groups=1
df$group_no <- print(sample(groups))
#For 4 groups (2 groups of 10 people and 2 groups of 5 people)
groups=c(rep(1,5), rep(2,5), rep(3,10), rep(4,10))
df$group_no <- print(sample(groups))
#For 7 groups (3 groups of 6 people and 4 groups of 3 people)
groups=c(rep(1,6), rep(2,6), rep(3,6), rep(4,3), rep(5,3), rep(6,3), rep(7,3))
df$group_no <- print(sample(groups))
#For 8 groups (6 groups of 4 people and 2 groups of 3 people)
groups=c(rep(1,4), rep(2,4), rep(3,4), rep(4,4), rep(5,4), rep(6,4), rep(7,3), rep(8,3))
df$group_no <- print(sample(groups))
#For 10 groups of 3 people each
groups=c(rep(1,3), rep(2,3), rep(3,3), rep(4,3), rep(5,3), rep(6,3), rep(7,3), rep(8,3), rep(9,3), rep(10,3))
df$group_no <- print(sample(groups))
fct_grouping <- function(df, nr_groups) {
?????
}
下面的代码应该按照您的要求执行,returns 一个带有分组的向量。
fct_grouping <- function(df, nr_groups) {
base_number <- floor(nrow(df) / nr_groups)
rest <- nrow(df) - base_number * nr_groups
groupings <- sort(c(rep(seq(nr_groups), base_number), if (rest==0) numeric() else seq(rest)))
return(groupings)
}
此功能使组大小尽可能接近并随机分配组。
grouper <- function(df, n) {
# create a random number for each row
random <- sample(1:nrow(df), replace = FALSE, nrow(df))
# divide the random number by the group size
df$group_number <- ceiling(random / (nrow(df) / n))
return(df)
}
我确定您要查找的内容在数学上应该可以用 R 进行编程,但是很难针对人数与组数的其余部分不等于零的情况进行建模因为有不止 1 个选项来分配案例(考虑定义 10 及以上的组数)。此外,您制作的示例不符合您要求的条件(最相似的组大小)。
这是我能想到的最接近的事情:
df <- data.frame(people = c(1:30))
fct_grouping <- function(df, nr_groups) {
if (nrow(df) %% nr_groups == 0) {
print(cbind(df, sample(nr_groups)))
} else {
print("n is not a multiple of number of people")
}}
df2 <- fct_grouping(df, 5)
# people sample(nr_groups)
# 1 1 1
# 2 2 3
# 3 3 2
# 4 4 5
# 5 5 4
# 6 6 1
# 7 7 3
# 8 8 2
# 9 9 5
# 10 10 4
# 11 11 1
# 12 12 3
# 13 13 2
# 14 14 5
# 15 15 4
# 16 16 1
# 17 17 3
# 18 18 2
# 19 19 5
# 20 20 4
# 21 21 1
# 22 22 3
# 23 23 2
# 24 24 5
# 25 25 4
# 26 26 1
# 27 27 3
# 28 28 2
# 29 29 5
# 30 30 4
我有一个 30 x 2 数据框 (df),其中一列包含 30 个人的姓名,第二列包含他们的 ID#。 我想在 R 中创建一个函数,将 30 个人随机且最均匀地分成几组,并且可以处理有余数和无余数的除法。
澄清一下,这个函数将:
• 取2个参数作为参数:df和一个代表组数的整数 • 将原始 df 还给我,但多了一个列,其中包含每个人随机分配到的组号 • 如果人数(行数)不能除以给定的整数,则剩余的行数应尽可能均匀地分配给各组
例如: • 如果我想将 30 人分成一组,我的函数应该 return df 有一个新列 "group_no",每个人都有 1 个(每个人将被分配到同一组)
• 如果我想要 4 个组,我希望看到 10 个人被分配到 2 个组,其余 5 个人被分配到另外 2 个组。
• 如果我想要 8 组,那么函数应该给我 6 组 4 人和 2 组 3 等等。
我已经编写了一些代码来满足我的需要,但我只是手动输入组,所以不只是它的随机性或正确性......我想改为将所有这些写在一个函数中可以自动执行这些任务:
#My code so far
#For 1 group of 30 people
people=1:30
groups=1
df$group_no <- print(sample(groups))
#For 4 groups (2 groups of 10 people and 2 groups of 5 people)
groups=c(rep(1,5), rep(2,5), rep(3,10), rep(4,10))
df$group_no <- print(sample(groups))
#For 7 groups (3 groups of 6 people and 4 groups of 3 people)
groups=c(rep(1,6), rep(2,6), rep(3,6), rep(4,3), rep(5,3), rep(6,3), rep(7,3))
df$group_no <- print(sample(groups))
#For 8 groups (6 groups of 4 people and 2 groups of 3 people)
groups=c(rep(1,4), rep(2,4), rep(3,4), rep(4,4), rep(5,4), rep(6,4), rep(7,3), rep(8,3))
df$group_no <- print(sample(groups))
#For 10 groups of 3 people each
groups=c(rep(1,3), rep(2,3), rep(3,3), rep(4,3), rep(5,3), rep(6,3), rep(7,3), rep(8,3), rep(9,3), rep(10,3))
df$group_no <- print(sample(groups))
fct_grouping <- function(df, nr_groups) {
?????
}
下面的代码应该按照您的要求执行,returns 一个带有分组的向量。
fct_grouping <- function(df, nr_groups) {
base_number <- floor(nrow(df) / nr_groups)
rest <- nrow(df) - base_number * nr_groups
groupings <- sort(c(rep(seq(nr_groups), base_number), if (rest==0) numeric() else seq(rest)))
return(groupings)
}
此功能使组大小尽可能接近并随机分配组。
grouper <- function(df, n) {
# create a random number for each row
random <- sample(1:nrow(df), replace = FALSE, nrow(df))
# divide the random number by the group size
df$group_number <- ceiling(random / (nrow(df) / n))
return(df)
}
我确定您要查找的内容在数学上应该可以用 R 进行编程,但是很难针对人数与组数的其余部分不等于零的情况进行建模因为有不止 1 个选项来分配案例(考虑定义 10 及以上的组数)。此外,您制作的示例不符合您要求的条件(最相似的组大小)。 这是我能想到的最接近的事情:
df <- data.frame(people = c(1:30))
fct_grouping <- function(df, nr_groups) {
if (nrow(df) %% nr_groups == 0) {
print(cbind(df, sample(nr_groups)))
} else {
print("n is not a multiple of number of people")
}}
df2 <- fct_grouping(df, 5)
# people sample(nr_groups)
# 1 1 1
# 2 2 3
# 3 3 2
# 4 4 5
# 5 5 4
# 6 6 1
# 7 7 3
# 8 8 2
# 9 9 5
# 10 10 4
# 11 11 1
# 12 12 3
# 13 13 2
# 14 14 5
# 15 15 4
# 16 16 1
# 17 17 3
# 18 18 2
# 19 19 5
# 20 20 4
# 21 21 1
# 22 22 3
# 23 23 2
# 24 24 5
# 25 25 4
# 26 26 1
# 27 27 3
# 28 28 2
# 29 29 5
# 30 30 4