Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:For循环只更新向量中的最后一个元素_R_For Loop_If Statement - Fatal编程技术网

R:For循环只更新向量中的最后一个元素

R:For循环只更新向量中的最后一个元素,r,for-loop,if-statement,R,For Loop,If Statement,我遵循的逻辑是 ->创建包含1000个元素的结果向量 ->使用for循环,在每次迭代中将旧的样本值“sorting_hat”替换为新值 ->使用if else语句将结果向量中的第i次迭代替换为依赖于新采样值“sorting_hat”的字符串 我希望这个向量的每个元素都被ifelse语句中四个字符串中的一个替换,而不仅仅是最后一个元素。目前什么让我感到困惑?试试: for (student_number in 1:total_students) 您应该指定for循环的范围尝试: for (s

我遵循的逻辑是

->创建包含1000个元素的结果向量

->使用for循环,在每次迭代中将旧的样本值“sorting_hat”替换为新值

->使用if else语句将结果向量中的第i次迭代替换为依赖于新采样值“sorting_hat”的字符串

我希望这个向量的每个元素都被ifelse语句中四个字符串中的一个替换,而不仅仅是最后一个元素。目前什么让我感到困惑?

试试:

 for (student_number in 1:total_students)
您应该指定for循环的范围

尝试:

 for (student_number in 1:total_students)

您应该指定for循环的范围

我们可以在这里尝试使用
ifelse
,但是从
dplyr
包中使用
case\u时可能更干净:

sorting_hat <- sample(x = 1:4, size = 1000, replace = TRUE, prob = c(9/41, 8/41, 14/41, 10/41))
all_student_houses <-
case_when (
    sorting_hat == 1 ~ "Slytherin",
    sorting_hat == 2 ~ "Gryffindor",
    sorting_hat == 3 ~ "Ravenclaw",
    TRUE ~ "Hufflepuff"
)

sorting\u hat我们可以在这里尝试使用
ifelse
,但是在
dplyr
包中使用
case\u时可能更干净:

sorting_hat <- sample(x = 1:4, size = 1000, replace = TRUE, prob = c(9/41, 8/41, 14/41, 10/41))
all_student_houses <-
case_when (
    sorting_hat == 1 ~ "Slytherin",
    sorting_hat == 2 ~ "Gryffindor",
    sorting_hat == 3 ~ "Ravenclaw",
    TRUE ~ "Hufflepuff"
)

sorting\u hat现在您的
for
循环运行一次。你应使用:

for (student_number in seq(1:total_students)) {
   ...
}

现在,您的
for
循环运行一次。你应使用:

for (student_number in seq(1:total_students)) {
   ...
}

您可以通过将代码替换为以下内容来优化代码

#Create a named vector 
student_number <- c('Slytherin' = 1,'Gryffindor' = 2, 'Ravenclaw' = 3,
                     'Hufflepuff' = 4)
#Create all the random numbers together
sorting_hat <- sample(x = 1:4, size = 1000, replace = TRUE, 
                         prob = c(9/41, 8/41, 14/41, 10/41))
#Replace it with names
all_student_houses <- names(student_number[sorting_hat])

#Check output
head(sorting_hat)
#[1] 2 2 2 4 3 2

head(all_student_houses)
#[1] "Gryffindor" "Gryffindor" "Gryffindor" "Hufflepuff" "Ravenclaw"  "Gryffindor"
#创建命名向量

学生编号您可以通过将代码替换为以下代码来优化代码

#Create a named vector 
student_number <- c('Slytherin' = 1,'Gryffindor' = 2, 'Ravenclaw' = 3,
                     'Hufflepuff' = 4)
#Create all the random numbers together
sorting_hat <- sample(x = 1:4, size = 1000, replace = TRUE, 
                         prob = c(9/41, 8/41, 14/41, 10/41))
#Replace it with names
all_student_houses <- names(student_number[sorting_hat])

#Check output
head(sorting_hat)
#[1] 2 2 2 4 3 2

head(all_student_houses)
#[1] "Gryffindor" "Gryffindor" "Gryffindor" "Hufflepuff" "Ravenclaw"  "Gryffindor"
#创建命名向量

student_number完全同意这个答案,因为它会更快,但我目前正在为我的团队编写一个R简介教程,并结合这个示例介绍ifelse、for循环等。正如你所看到的,我还在工作中学习!我认为,你应该从一开始就教授你的学期最佳实践。但是,使用循环本身并没有什么坏处。完全同意这个答案,因为它会更快。不过,我目前正在为我的团队编写一个R简介教程,并结合这个示例介绍ifelse、for循环等。正如你所看到的,我还在工作中学习!我认为,你应该从一开始就教授你的学期最佳实践。但是,使用循环本身并没有什么坏处。