for 循环中的用户定义函数
User defined function within for-loops
我正在做一个项目,我在 6 周内模拟 8 个课堂社交网络,所以 30 次迭代。学生将根据许多因素相互提名,我计划模拟一些条件,在这些条件下我将其中一些因素删除或添加到模拟中。换句话说,我将重复大量代码,所以我宁愿使用函数,也不愿尽可能地剪切和粘贴。
现在,我正在尝试创建一个函数,根据他们情绪的相似性来调整一个学生 select 对另一个学生的概率。当我将它包含在一组嵌套的 for 循环中时,效果很好:
num_students <- 5
names_students <- letters[1:num_students]
student_emotion <- sample(round(runif(5, min = -5, max = 5), digits = 1))
student_emotion_df <- cbind.data.frame(names_students, student_emotion)
probs <- rep(1/num_students, 5)
row_prob <- vector(length = 5)
for(i in 1:num_students){
for(q in 1:num_students){
if(abs(student_emotion[i]-student_emotion[q]) >= 0 &
abs(student_emotion[i]-student_emotion[q]) <= .5){
row_prob[q] <- 1*probs[q]
} else if(abs(student_emotion[i]-student_emotion[q]) > .5 &
abs(student_emotion[i]-student_emotion[q]) <= 1) {
row_prob[q] <- .75 * probs[q]
}
else {
row_prob[q] <- .5 * probs[q]
}
}
}
row_prob 对象是列中学生 i 与行中 select 学生 q 的概率向量。
我基于相同的代码创建了一个用户定义的函数,并且有效:
emotion_difference_fun <- function(probs){
for(q in 1:num_students){
if(abs(student_emotion[i]-student_emotion[q]) >= 0 &
abs(student_emotion[i]-student_emotion[q]) <= .5){
row_prob[q] <- 1*probs[q]
} else if(abs(student_emotion[i]-student_emotion[q]) > .5 &
abs(student_emotion[i]-student_emotion[q]) <= 1) {
row_prob[q] <- .75 * probs[q]
}
else {
row_prob[q] <- .5 * probs[q]
}
}
return(row_prob)
}
emotion_difference_fun(probs)
但是当我尝试将该函数嵌入到循环遍历列的 for 循环中时,row_prob returns 作为一个空向量:
for(i in 1:num_students){
emotion_difference_fun(probs)
}
关于如何让它工作有什么想法吗?
感谢您提供的任何帮助。
如果我正确理解了你的问题,那么你需要在最后一个 'for' 循环中分配结果:
for(i in 1:num_students){
if(i == 1) out <- NULL
out <- c(out, emotion_difference_fun(probs))
}
out
这就是你要找的吗?
不过我不清楚的是,为什么在您的第二个代码部分中您没有寻找 5*5 矩阵。最终,当 运行 那个代码时,你为 i = 5 个学生做的并不重要,因为它只会保存在 row_prob 你最后一次迭代(学生 = 5)。
您可以使用 replicate
为 num_students
重复函数 emotion_difference_fun
。
result <- replicate(num_students, emotion_difference_fun(probs))
您还可以设置 simplify = FALSE
以列表形式输出。
result <- replicate(num_students, emotion_difference_fun(probs),simplify = FALSE)
我正在做一个项目,我在 6 周内模拟 8 个课堂社交网络,所以 30 次迭代。学生将根据许多因素相互提名,我计划模拟一些条件,在这些条件下我将其中一些因素删除或添加到模拟中。换句话说,我将重复大量代码,所以我宁愿使用函数,也不愿尽可能地剪切和粘贴。
现在,我正在尝试创建一个函数,根据他们情绪的相似性来调整一个学生 select 对另一个学生的概率。当我将它包含在一组嵌套的 for 循环中时,效果很好:
num_students <- 5
names_students <- letters[1:num_students]
student_emotion <- sample(round(runif(5, min = -5, max = 5), digits = 1))
student_emotion_df <- cbind.data.frame(names_students, student_emotion)
probs <- rep(1/num_students, 5)
row_prob <- vector(length = 5)
for(i in 1:num_students){
for(q in 1:num_students){
if(abs(student_emotion[i]-student_emotion[q]) >= 0 &
abs(student_emotion[i]-student_emotion[q]) <= .5){
row_prob[q] <- 1*probs[q]
} else if(abs(student_emotion[i]-student_emotion[q]) > .5 &
abs(student_emotion[i]-student_emotion[q]) <= 1) {
row_prob[q] <- .75 * probs[q]
}
else {
row_prob[q] <- .5 * probs[q]
}
}
}
row_prob 对象是列中学生 i 与行中 select 学生 q 的概率向量。
我基于相同的代码创建了一个用户定义的函数,并且有效:
emotion_difference_fun <- function(probs){
for(q in 1:num_students){
if(abs(student_emotion[i]-student_emotion[q]) >= 0 &
abs(student_emotion[i]-student_emotion[q]) <= .5){
row_prob[q] <- 1*probs[q]
} else if(abs(student_emotion[i]-student_emotion[q]) > .5 &
abs(student_emotion[i]-student_emotion[q]) <= 1) {
row_prob[q] <- .75 * probs[q]
}
else {
row_prob[q] <- .5 * probs[q]
}
}
return(row_prob)
}
emotion_difference_fun(probs)
但是当我尝试将该函数嵌入到循环遍历列的 for 循环中时,row_prob returns 作为一个空向量:
for(i in 1:num_students){
emotion_difference_fun(probs)
}
关于如何让它工作有什么想法吗?
感谢您提供的任何帮助。
如果我正确理解了你的问题,那么你需要在最后一个 'for' 循环中分配结果:
for(i in 1:num_students){
if(i == 1) out <- NULL
out <- c(out, emotion_difference_fun(probs))
}
out
这就是你要找的吗?
不过我不清楚的是,为什么在您的第二个代码部分中您没有寻找 5*5 矩阵。最终,当 运行 那个代码时,你为 i = 5 个学生做的并不重要,因为它只会保存在 row_prob 你最后一次迭代(学生 = 5)。
您可以使用 replicate
为 num_students
重复函数 emotion_difference_fun
。
result <- replicate(num_students, emotion_difference_fun(probs))
您还可以设置 simplify = FALSE
以列表形式输出。
result <- replicate(num_students, emotion_difference_fun(probs),simplify = FALSE)