正确存储R中的while循环结果

huangapple go评论76阅读模式
英文:

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 &lt;- c(213,214,215,216)
R &lt;- c(0.11937253,0.11937253, 0.11937253,0.11937253)
P &lt;- 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 &lt;- function(N,R,P) {
err &lt;- c()
i = 2
while ((N[i-1]+R[i-1]+P[i-1]) &lt; 220) {
if (is.na(N[i])) {
break {
} else {
err &lt;- 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 &lt;- c(213,214,215,216)
R &lt;- c(0.11937253,0.11937253, 0.11937253,0.11937253)
P &lt;- c(0.549252447, 0.549252447, 0.549252447, 0.549252447)


tmp &lt;- function(N,R,P) {
  err &lt;- c()
  i = 2
  while ((N[i-1]+R[i-1]+P[i-1]) &lt; 220) {
    if (is.na(N[i])) {
      break 
      } else {
        e &lt;- N[i]/(R[i]+P[i])
        err &lt;- c(err, e)
        i = i + 1
        print(e)
        }
  }
  return(err)
}

out &lt;- tmp(N, R, P)
#&gt; [1] 320.0598
#&gt; [1] 321.5554
#&gt; [1] 323.051

out
#&gt; [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 &lt;- function(N, R, P) {
  s &lt;- N + R + P
  i &lt;- which(s &lt; 220)
  (N[i] / (R[i] + P[i]))[-1]
}

tmp(N, R, P)

# [1] 320.06 321.56 323.05

huangapple
  • 本文由 发表于 2023年4月6日 19:49:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定