在if语句中获取“参数长度为0”

问题描述

我正在尝试计算14天回溯期的RSI。我的excel文件中有三列-DATE,OPEN,CLOSE。但是,当我在if语句中检查dff> 0时,会引发错误-if(dff> 0){:参数长度为零。下面是我的代码。有什么建议为什么会发生这种情况?

library(readxl)
data <- read_excel(address of file)
data <- data.frame(data)
rows <- nrow(data)
RSI <- rep(0,rows)
for(i in 14:rows){
  gain <- 0
  cnt_gain <- 0
  cnt_loss <- 0
  loss <- 0
  for(j in i-13:i){
    dff <- data[j,3]-data[j,2]
    if(dff > 0){
      gain <- gain + dff
      cnt_gain <- cnt_gain + 1
    }
    else{
      loss <- loss -dff
      cnt_loss <- cnt_loss + 1
    }
  }
  avg_gain <- gain/cnt_gain
  avg_loss <- loss/cnt_loss
  RSI[i] <- 100 - 100/(1+ avg_gain/avg_loss)
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)