英文:
Properly store while loop result in R
问题
I have three vectors:
R <- c(0.11937253,0.11937253, 0.11937253,0.11937253)
P <- c(0.549252447, 0.549252447, 0.549252447, 0.549252447)
from these vectors, I am trying to get a result using a customized function with a nested while loop:
tmp <- function(N,R,P) {
err <- c()
i = 2
while ((N[i-1]+R[i-1]+P[i-1]) < 220) {
if (is.na(N[i])) {
break {
} else {
err <- N[i]/(R[i]+P[i])
i = i + 1
print(err)
}
}
The focus is on storing err
. Using print
I can properly see my results (which should be a vector), but when I use return
R gives me only the first element of the iteration. How can I get the proper output? It should be a vector containing all the elements coming from the iteration.
NB. Don’t focus on the condition, it’s just a proxy, the real one is another but I checked it and there is no problem there. Moreover, I built an if statement since the condition of the while loop is very rare and can be it never happens and it would iterate forever.
英文:
I have three vectors:
N <- c(213,214,215,216)
R <- c(0.11937253,0.11937253, 0.11937253,0.11937253)
P <- c(0.549252447, 0.549252447, 0.549252447, 0.549252447)
from this vectors I am trying to get a result using a customized function with a nested while loop:
tmp <- function(N,R,P) {
err <- c()
i = 2
while ((N[i-1]+R[i-1]+P[i-1]) < 220) {
if (is.na(N[i])) {
break {
} else {
err <- N[i]/(R[i]+P[i])
i = i + 1
print(err)
}
}
The focus is on storing err
. Using print
I can properly see my results (which should be a vector), but when I use return
R gives me only the first element of the iteration. How can I get the proper output? It should be a vector containing all the elements coming from the iteration.
NB. Don’t focus on the condition, it’s just a proxy, the real one is another but I checked it and there is no problem there. Moreover, I built an if statement since the the condition of the while loop is very rare and can be it never happens and it would iterate for ever.
答案1
得分: 1
print()
函数将结果发送到控制台,但不以任何其他方式保存。您需要按照评论中建议的方式组合使用。需要在每次迭代中不断添加到err
,然后在函数结束时返回它。
N <- c(213,214,215,216)
R <- c(0.11937253,0.11937253, 0.11937253,0.11937253)
P <- c(0.549252447, 0.549252447, 0.549252447, 0.549252447)
tmp <- function(N,R,P) {
err <- c()
i = 2
while ((N[i-1]+R[i-1]+P[i-1]) < 220) {
if (is.na(N[i])) {
break
} else {
e <- N[i]/(R[i]+P[i])
err <- c(err, e)
i = i + 1
print(e)
}
}
return(err)
}
out <- tmp(N, R, P)
out
# [1] 320.0598 321.5554 323.0510
创建于2023-04-06,使用 reprex v2.0.2
英文:
The print()
function sends a result to the console, but doesn't save it in any other way. You need a combination of what was suggested in the comments. You need to keep adding to err
each iteration and then return it at the end of the function.
N <- c(213,214,215,216)
R <- c(0.11937253,0.11937253, 0.11937253,0.11937253)
P <- c(0.549252447, 0.549252447, 0.549252447, 0.549252447)
tmp <- function(N,R,P) {
err <- c()
i = 2
while ((N[i-1]+R[i-1]+P[i-1]) < 220) {
if (is.na(N[i])) {
break
} else {
e <- N[i]/(R[i]+P[i])
err <- c(err, e)
i = i + 1
print(e)
}
}
return(err)
}
out <- tmp(N, R, P)
#> [1] 320.0598
#> [1] 321.5554
#> [1] 323.051
out
#> [1] 320.0598 321.5554 323.0510
<sup>Created on 2023-04-06 with reprex v2.0.2</sup>
答案2
得分: 0
tmp <- function(N, R, P) {
s <- N + R + P
i <- which(s < 220)
(N[i] / (R[i] + P[i]))[-1]
}
tmp(N, R, P)
[1] 320.06 321.56 323.05
英文:
tmp <- function(N, R, P) {
s <- N + R + P
i <- which(s < 220)
(N[i] / (R[i] + P[i]))[-1]
}
tmp(N, R, P)
# [1] 320.06 321.56 323.05
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论