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

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

Properly store while loop result in R

问题

I have three vectors:

  1. R <- c(0.11937253,0.11937253, 0.11937253,0.11937253)
  2. 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:

  1. tmp <- function(N,R,P) {
  2. err <- c()
  3. i = 2
  4. while ((N[i-1]+R[i-1]+P[i-1]) < 220) {
  5. if (is.na(N[i])) {
  6. break {
  7. } else {
  8. err <- N[i]/(R[i]+P[i])
  9. i = i + 1
  10. print(err)
  11. }
  12. }

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:

  1. N &lt;- c(213,214,215,216)
  2. R &lt;- c(0.11937253,0.11937253, 0.11937253,0.11937253)
  3. 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:

  1. tmp &lt;- function(N,R,P) {
  2. err &lt;- c()
  3. i = 2
  4. while ((N[i-1]+R[i-1]+P[i-1]) &lt; 220) {
  5. if (is.na(N[i])) {
  6. break {
  7. } else {
  8. err &lt;- N[i]/(R[i]+P[i])
  9. i = i + 1
  10. print(err)
  11. }
  12. }

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,然后在函数结束时返回它。

  1. N <- c(213,214,215,216)
  2. R <- c(0.11937253,0.11937253, 0.11937253,0.11937253)
  3. P <- c(0.549252447, 0.549252447, 0.549252447, 0.549252447)
  4. tmp <- function(N,R,P) {
  5. err <- c()
  6. i = 2
  7. while ((N[i-1]+R[i-1]+P[i-1]) < 220) {
  8. if (is.na(N[i])) {
  9. break
  10. } else {
  11. e <- N[i]/(R[i]+P[i])
  12. err <- c(err, e)
  13. i = i + 1
  14. print(e)
  15. }
  16. }
  17. return(err)
  18. }
  19. out <- tmp(N, R, P)
  20. out
  21. # [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.

  1. N &lt;- c(213,214,215,216)
  2. R &lt;- c(0.11937253,0.11937253, 0.11937253,0.11937253)
  3. P &lt;- c(0.549252447, 0.549252447, 0.549252447, 0.549252447)
  4. tmp &lt;- function(N,R,P) {
  5. err &lt;- c()
  6. i = 2
  7. while ((N[i-1]+R[i-1]+P[i-1]) &lt; 220) {
  8. if (is.na(N[i])) {
  9. break
  10. } else {
  11. e &lt;- N[i]/(R[i]+P[i])
  12. err &lt;- c(err, e)
  13. i = i + 1
  14. print(e)
  15. }
  16. }
  17. return(err)
  18. }
  19. out &lt;- tmp(N, R, P)
  20. #&gt; [1] 320.0598
  21. #&gt; [1] 321.5554
  22. #&gt; [1] 323.051
  23. out
  24. #&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

英文:
  1. tmp &lt;- function(N, R, P) {
  2. s &lt;- N + R + P
  3. i &lt;- which(s &lt; 220)
  4. (N[i] / (R[i] + P[i]))[-1]
  5. }
  6. tmp(N, R, P)
  7. # [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:

确定