Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中,参数为;for loop";_R - Fatal编程技术网

在R中,参数为;for loop";

在R中,参数为;for loop";,r,R,有人能告诉我下面的R代码有什么问题吗 i = 1.001 #make SAV and STO become vector SAV = c() STO = c() #set the initial values for the vector SAV[1] = 0 STO[1] = 100 for (t in 2:1000) { if ((price[t]>9.9)&(price[t]<10.1)&(SAV[t-1]!=0)) SAV[t]=SAV[t-1

有人能告诉我下面的R代码有什么问题吗

i = 1.001
#make SAV and STO become vector
SAV = c()
STO = c()
#set the initial values for the vector
SAV[1] = 0
STO[1] = 100


for (t in 2:1000) {
if ((price[t]>9.9)&(price[t]<10.1)&(SAV[t-1]!=0))
      SAV[t]=SAV[t-1]*i 
      STO[t]=0 
}

for (t in 2:1000) {
if ((price[t]>9.9)&(price[t]<10.1)&(SAV[t-1]=0))
      STO[t] = STO [t-1]
      SAV[t] = 0
}

SAV
STO
i=1.001
#使SAV和STO成为向量
SAV=c()
STO=c()
#设置向量的初始值
SAV[1]=0
STO[1]=100
对于(t在2:1000中){

如果((price[t]>9.9)&(price[t]9.9)&(price[t]I不是很好的一个R,但是可能数组从0开始?(而不是从1开始)

==>

我的第二个猜测是关于for循环中的if条件。我会在整个表达式上加上括号,类似这样

for (t in 2:1000) {
  if ((price[t]>9.9)&(price[t]<10.1)&(SAV[t-1]=0)) {
      STO[t] = STO [t-1]
      SAV[t] = 0
  }
}
for(2:1000中的t){

如果((price[t]>9.9)和(price[t]我认为每次迭代都会覆盖向量STO和SAV。很难说,因为价格向量尚未声明。尝试将STO和SAV初始化为所需长度的向量,而不是0长度的向量:

SAV=矩阵(0,11000)


STO=matrix(0,11000)

我将尝试以下方法。修改它以与程序逻辑一致

for (t in 2:1000) {
        if ((price[t]>9)&(price[t]<10)) {
             # values for STO,SAV when price in the interval and SAV[t-1]!=0
             if (SAV[t-1]!=0) { 
                SAV[t]=SAV[t-1]*i 
                STO[t]=0
             }
             # values for STO,SAV when price in the interval and SAV[t-1]==0
             else { 
               STO[t] = STO[t-1]
               SAV[t] = 0 
             }
        }
        # values for STO,SAV when price not in the interval
        else {   
           STO[t] = STO[t-1]
           SAV[t] = 1
        }
}
for(2:1000中的t){

如果((price[t]>9)和((price[t])实际上,价格是向量,那么主要的问题是((t in 2:1000){如果((price[t]>9.9)和((price[t]thx)对于你的帮助,我认为主要的问题是我不应该把它们放在两个for循环中……非常感谢你,你的if块周围缺少了小括号。
for (t in 2:1000) {
  if ((price[t]>9.9)&(price[t]<10.1)&(SAV[t-1]=0)) {
      STO[t] = STO [t-1]
      SAV[t] = 0
  }
}
for (t in 2:1000) {
        if ((price[t]>9)&(price[t]<10)) {
             # values for STO,SAV when price in the interval and SAV[t-1]!=0
             if (SAV[t-1]!=0) { 
                SAV[t]=SAV[t-1]*i 
                STO[t]=0
             }
             # values for STO,SAV when price in the interval and SAV[t-1]==0
             else { 
               STO[t] = STO[t-1]
               SAV[t] = 0 
             }
        }
        # values for STO,SAV when price not in the interval
        else {   
           STO[t] = STO[t-1]
           SAV[t] = 1
        }
}