jeudi 11 octobre 2018

Getting wrong result from IF code which condition contains for-loop

I want to set some conditions for IF, which has for-loop inside. The whole map of my function is like this:

  1. If conditions:

    (1) check the file name "ABC.cor" if it exists in my working directory

    (2) extract a scalar ('temcor$maxgrad') from a list named 'temcor' and compare with the value of 0.02

    (3) get a numeric vector('temcor$est') from the list 'temcor' and compare the each values with the first and second column in the matrix named 'comp.value' using for-loop. If it's true, then assign TRUE to a new vector named 'range_val'.

  2. corresponding codes:

    If the condition is TRUE: I want to add 1 to k

    If the condition is FALSE: I want to add 1 to no.Failures

I was expecting to get k with the value of 1, which indicates that the condition is true, but I get no.Failures with the value of 1.

Can you please help me with this problem?

I attached my code and the results

code

comp.value<-matrix(c(-6.9, 14.221, -15.0, -11.0, 0.69, 14.914, -9.0, 0.69),ncol=2, byrow=F)

      k<-0
      no.Failures<-0
      #temcor$est<-c(-3.2767, 14.5300, -11.5610, -0.9686)
      #temcor$maxgrad=0.008
if(file.exists(paste0('ABC',".cor")) && temcor$maxgrad <=0.02 && isTRUE( for (j in 1:4) {


if(comp.value[j,1] < temcor$est[j] && temcor$est[j] < comp.value[j,2]){
      range_val[j]<-TRUE
    }else{
      range_val[j]<-FALSE
    }
  #if(isTRUE(range_val)){
  #  print(range_val)
  #  break;
  #}
  })) {
    k<-k+1
  }else{
    no.Failures<-no.Failures+1
  }

result

> k
[1] 0
>   no.Failures
[1] 1

Aucun commentaire:

Enregistrer un commentaire