0

I am trying to apply an if condition to each element of vectors in R, but seems that I am doing something wrong:

nobjects <- length(EMATRSTDV)
for (i in 1:nobjects) {
 if (MPEMAPlusDemiTR - EMATRSTDV > Cl(myData$AAPL)){
 ShortLevel <- MPEMAPlusDemiTR - EMATRSTDV
 } else {
 ShortLevel <- "..."
 }
}

I am getting the error message:

Error in if (MPEMAPlusDemiTR - EMATRSTDV > Cl(myData$AAPL)) { : missing value where TRUE/FALSE needed In addition: Warning message: In if (MPEMAPlusDemiTR - EMATRSTDV > Cl(myData$AAPL)) { : the condition has length > 1 and only the first element will be used

RHertel
  • 23,412
  • 5
  • 38
  • 64

1 Answers1

0

the condition has length > 1 and only the first element will be used

well, from the error message and from the code where you compute length(EMATRSTDV) it is obvious that EMATRSTDV is a vector. In condition you have to use indexed value

for (i in 1:nobjects) {
    if (MPEMAPlusDemiTR - EMATRSTDV[i] > Cl(myData$AAPL)){
        ShortLevel[i] <- MPEMAPlusDemiTR - EMATRSTDV[i]
    } else {
        ShortLevel[i] <- "..."
    }
}
Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64