R嵌套if语句错误“;条件具有长度>;1且仅使用第一个元素“;

R嵌套if语句错误“;条件具有长度>;1且仅使用第一个元素“;,r,if-statement,nested,R,If Statement,Nested,我试图在R中执行许多条件事件,但我得到了警告: Warning messages: 1: In if (closeV > openV) { : the condition has length > 1 and only the first element will be used 2: In if ((highV - closeV) < Minimum) { : the condition has length > 1 and only the first ele

我试图在R中执行许多条件事件,但我得到了警告:

Warning messages:
1: In if (closeV > openV) { :
  the condition has length > 1 and only the first element will be used
2: In if ((highV - closeV) < Minimum) { :
  the condition has length > 1 and only the first element will be used
3: In if ((openV - lowV) > Threshold) { :
  the condition has length > 1 and only the first element will be used
4: In if (((openV - lowV) < Threshold)) { :
  the condition has length > 1 and only the first element will be used
5: In if ((closeV - openV) < Threshold) { :
  the condition has length > 1 and only the first element will be used
6: In if ((closeV - lowV) < (Threshold * 2)) { :
  the condition has length > 1 and only the first element will be used
警告消息:
1:在if(closeV>openV){:
条件的长度大于1,并且只使用第一个元素
2:在if((高V-低V)<最小值){:
条件的长度大于1,并且只使用第一个元素
3:在if中((openV-lowV)>阈值){:
条件的长度大于1,并且只使用第一个元素
4:在if中((openV-lowV)<阈值)){:
条件的长度大于1,并且只使用第一个元素
5:在if((closeV-openV)<阈值){:
条件的长度大于1,并且只使用第一个元素
6:在if((闭合电压-低电压)<(阈值*2)){:
条件的长度大于1,并且只使用第一个元素
这是一个巨大的ifs巢穴,它现在还没有优化,但我不能让它工作,因为那个警告。 在这个函数中有大约40个ifs,你知道我需要做什么来绕过这个警告吗? 代码看起来像这样

  if(closeV>openV)#1 First we check if we have a positive value
  {
    if((highV-closeV)<Minimum)
    {
      if((openV-lowV) >Threshold)
      {
        if((closeV-openV)<Threshold)
        {
          #3.1 This is a Hammer with positive movement
          if((closeV-lowV)<(Threshold*2))
          {
            #3.1.1 not much movement
            return(X*2)
          }
          else if((closeV-lowV)>(Treshold*2))
          {
            #3.1.2 a lot of movement
            return(X*3)
          }

        }
        else if((closeV-openV)>Threshold)
        {
          #3.2 Hammer but with a lot of movement 
          if((closeV-lowV)<(Threshold*2))
          {
            #3.2.1 not much movement
            return(X)
          }
          else if((closeV-lowV)>(Treshold*2))
          {
            #3.2.2 a lot of movement
            return(X*5)
          }
        }        

      }
      else if(((openV-lowV)<Threshold)
if(closeV>openV)#1首先检查是否有正值
{
如果((高电压关闭)阈值)
{
如果((关闭/打开)阈值)
{
#3.2锤子,但运动频繁
如果((关闭低电压)(Treshold*2))
{
#3.2.2大量移动
返回(X*5)
}
}        
}

else if((openV lowV)问题不在于嵌套的if语句,而在于输入到它们中的数据结构:警告告诉您,比较运算符仅应用于输入到if语句中的数据结构的第一个元素

抛出与您收到的警告消息相同的消息

a = seq(1, 10, 1)
b = seq(0, 18, 2)

for (i in 1:10) {
  if (a[i]>b[i]){
    print(a[i])
  } else{
    print(b[i])
  }
}
相比之下,评估工作进展顺利


另外,请注意,虽然这两段代码都经过了计算,但结果却截然不同。

哦,你说得对,我在那里发送了错误的信息,谢谢!错误是我发送了一个数据数组,我只是检查了一个没有在任何地方指定的值。。。
a = seq(1, 10, 1)
b = seq(0, 18, 2)

for (i in 1:10) {
  if (a[i]>b[i]){
    print(a[i])
  } else{
    print(b[i])
  }
}