在R中出现错误,丢失了TRUE/FALSE需要的值[复制]

时间:2022-08-28 22:28:44

This question already has an answer here:

这个问题已经有了答案:

The following is part of my R script:

以下是我的R脚本的一部分:

for (i in 1:N-1) {
  if (-50<nw.bank[i] && 50>nw.bank[i]) {
      rl[i+1] <- (rl[i]+0.001)
  } 
    else {
      rl[i+1] <- rl[i]
  }
}

When run, I get the following message:

当运行时,我得到如下信息:

"Error in if (-50 < nw.bank[i] && 50 > nw.bank[i]) { : 
  missing value where TRUE/FALSE needed"

Can anyone help me out? Thank you so much!

有人能帮帮我吗?谢谢你这么多!

best, hyun

最好,hyun

3 个解决方案

#1


4  

Note that 1:N-1 is parsed as (1:N) - 1. This means your loop is iterating over 0, 1, 2, ... (N - 1), and the test using nw.bank[i] will error out on the first iteration. You probably meant 1:(N - 1).

注意,1:N-1被解析为(1:N) -1。这意味着循环在0、1、2、…(N - 1)和nw的测试。银行[i]将在第一次迭代中出错。你可能是指1:(N - 1)。

#2


0  

If it's not the problem with 1:N-1 as Hong suggested, then try to find if there is NA in nw.bank using any(is.na(nw.bank)), and use & instead of && unless you have a good reason (see R - boolean operators && and ||).

如果不是1:N-1的问题,那么试着找出nw中是否有NA。使用任何(is.na(nw.bank)),使用&而不是&&除非你有充分的理由(见R -布尔运算符&和||)。

#3


0  

R for loops run slower when compared to code that handles vectors directly.

与直接处理向量的代码相比,for循环运行速度较慢。

This procedure you give can be vectorized as follows:

你给出的这个过程可以量化如下:

Get some random values for nw.bank so the calculation can be demonstrated:

为nw获取一些随机值。银行的计算可以证明:

nw.bank<- 200*runif(20)-100 

[1]  43.273799  19.051499  37.552510  76.940632 -59.176684 -27.379326
[7] -37.512520  77.776610  88.127792 -91.213580 -50.691943  78.697820
[13]  36.933503 -76.973450  28.143336 -55.136574 -70.693362 -14.213375
[19]  15.666707  -3.072321

Note there is only one ampersand (&) in a vectorized AND

注意,在一个矢量图中只有一个&和(&)。

as.numeric(((-50<nw.bank) & (50>nw.bank)))
   [1] 1 1 1 0 0 1 1 0 0 0 0 0 1 0 1 0 0 1 1 1

Now multiply by 0.001 and take cumulative sum

现在乘以0。001,取累积和。

   cumsum(0.001*as.numeric(((-50<nw.bank) & (50>nw.bank))))

[1] 0.001 0.002 0.003 0.003 0.003 0.004 0.005 0.005 0.005 0.005 0.005 0.005
[13] 0.006 0.006 0.007 0.007 0.007 0.008 0.009 0.010

Now you need an adjustable initial value, perhaps, and you are done

现在您需要一个可调整的初始值,也许,您已经完成了。

rlinit <- 3.0; 
rl <- cumsum(0.001*as.numeric(((-50<nw.bank) & (50>nw.bank)))) + rlinit



[1] 3.001 3.002 3.003 3.003 3.003 3.004 3.005 3.005 3.005 3.005 3.005 3.005
[13] 3.006 3.006 3.007 3.007 3.007 3.008 3.009 3.010

#1


4  

Note that 1:N-1 is parsed as (1:N) - 1. This means your loop is iterating over 0, 1, 2, ... (N - 1), and the test using nw.bank[i] will error out on the first iteration. You probably meant 1:(N - 1).

注意,1:N-1被解析为(1:N) -1。这意味着循环在0、1、2、…(N - 1)和nw的测试。银行[i]将在第一次迭代中出错。你可能是指1:(N - 1)。

#2


0  

If it's not the problem with 1:N-1 as Hong suggested, then try to find if there is NA in nw.bank using any(is.na(nw.bank)), and use & instead of && unless you have a good reason (see R - boolean operators && and ||).

如果不是1:N-1的问题,那么试着找出nw中是否有NA。使用任何(is.na(nw.bank)),使用&而不是&&除非你有充分的理由(见R -布尔运算符&和||)。

#3


0  

R for loops run slower when compared to code that handles vectors directly.

与直接处理向量的代码相比,for循环运行速度较慢。

This procedure you give can be vectorized as follows:

你给出的这个过程可以量化如下:

Get some random values for nw.bank so the calculation can be demonstrated:

为nw获取一些随机值。银行的计算可以证明:

nw.bank<- 200*runif(20)-100 

[1]  43.273799  19.051499  37.552510  76.940632 -59.176684 -27.379326
[7] -37.512520  77.776610  88.127792 -91.213580 -50.691943  78.697820
[13]  36.933503 -76.973450  28.143336 -55.136574 -70.693362 -14.213375
[19]  15.666707  -3.072321

Note there is only one ampersand (&) in a vectorized AND

注意,在一个矢量图中只有一个&和(&)。

as.numeric(((-50<nw.bank) & (50>nw.bank)))
   [1] 1 1 1 0 0 1 1 0 0 0 0 0 1 0 1 0 0 1 1 1

Now multiply by 0.001 and take cumulative sum

现在乘以0。001,取累积和。

   cumsum(0.001*as.numeric(((-50<nw.bank) & (50>nw.bank))))

[1] 0.001 0.002 0.003 0.003 0.003 0.004 0.005 0.005 0.005 0.005 0.005 0.005
[13] 0.006 0.006 0.007 0.007 0.007 0.008 0.009 0.010

Now you need an adjustable initial value, perhaps, and you are done

现在您需要一个可调整的初始值,也许,您已经完成了。

rlinit <- 3.0; 
rl <- cumsum(0.001*as.numeric(((-50<nw.bank) & (50>nw.bank)))) + rlinit



[1] 3.001 3.002 3.003 3.003 3.003 3.004 3.005 3.005 3.005 3.005 3.005 3.005
[13] 3.006 3.006 3.007 3.007 3.007 3.008 3.009 3.010