코드에 다시 문제가 있습니다. 여기에 질문이 있는데 지금 당장 가지고있는 것이 있습니다 :R - for, while 및 repeat 루프를 사용하여 값 인쇄
# 2. Draw a random sample of size n=20 from a uniform distribution in 0 and 1 using
# runif(20). Sequentially, print values using the following rules:
# i. Print a value if it is less than 0.3 or more than 0.8, but skip it
# (don’t print the value) if it is in (0.1, 0.2],
# ii. Skip the entire process if you find a value in [0.4,0.5].
# Write three separate R codes using (a) for loop, (b) while loop
# and (c) repeat loop.
# (a) for loop
n = runif(20)
for (val in n){
if (val > 0.1 & val <= 0.2){
next
} else if (val < 0.3 | val > 0.8){
print(val)
} else if (val >= 0.4 & val <= 0.5){
print(val)
break
}
}
# (b) while loop
n = 1
m = runif(20)
while(n < 20){
if (m > 0.1 & m <= 0.2){
next
} else if (m < 0.3 | m > 0.8){
print(m)
} else if (m >= 0.4 & m <= 0.5){
print(m)
break
}
n = n + 1
}
# (c) repeat loop
n = 1
m = runif(20)
repeat{
if (m > 0.1 & m <= 0.2){
next
} else if (m < 0.3 | m > 0.8){
print(val)
} else if (m >= 0.4 & m <= 0.5){
print(m)
break
}
}
(a) for 루프가 완벽하게 작동합니다.
내 유일한 문제는 (b) while 루프 및 (c) 반복 루프입니다. 그는 수업이나 노트에서 좋은 일을하지 않았으며 while 루프와 repeat 루프를 반복합니다. 도와주세요.
자신의 말로는 자신이하고있는 일, 수행하고자하는 일 및 잘못되는 것이 무엇인지 설명 할 수 있습니까? –
(1) 'm'에 대한 색인을 생성하지 않았습니다. (2)'repeat' 루프에서는 인덱스'n'을 증가시키지 않습니다. (3)'next'를 할 때 다음 명령이 루프의 최상위에 있으므로'n <- n + 1'은 * 실행되지 않습니다. –
@RuiBarradas while 루프와 repeat 루프는 어떻게 수정합니까? – Renee