jeudi 28 janvier 2021

If condition does not recognize a true statement

I made a new relation that works fine alone, but not in a function.

I create an inequality that includes infinity:

inequality <- function(t1,t2){
  if (t1<=t2) {
    TRUE
  }
  else if (t2 == "inf"){
    TRUE
  }
  else {
    FALSE
  }
}

This code works fine. For example inequality(9,12) returns TRUE. So I tried to use it to create a new function.

The arguments of this function are a vector (SomeSeq) with a part in an ascending order (from v1 until v2) and an other a part in an ascending order (from v2+1 until v3). The function has to put them in an ascending order (from v1 until v3).

I tried this function with MySeq1 <- c(4,6, 2,5,7,9, 3,4,7,12,21, 6,5,0), so I wrote UnionSeq(MySeq1,3,6,11). It dint't works well. So I put in my code the command:

print(paste0("k:", k, " i:", i, " j:", j , " L:", left[i]," R:", right[j], " Sec:", SomeSeq[k], inequality(left[i],right[j])))

As you can see it returns "k:9 i:4 j:4 L:9 R:12 Sec:12FALSE", but, as I said inequality(9,12) returns TRUE.

Why is that? Am I doing something wrong?

The full code is below:

UnionSeq <- function(SomeSeq,v1,v2,v3){
  n1 <- v2-v1+1
  n2 <- v3-v2
  left <- c(1)
  for (i in c(1:n1)){
    left[i] <- SomeSeq[v1+i-1]
  }
  left[n1+1] <- "inf"
  right <- c(1)
  for (i in c(1:n2)){
    right[i] <- SomeSeq[v2+i]
  }
  right[n2+1] <- "inf"
  i <- 1
  j <- 1
  for (k in c(v1:v3)){
    if (inequality(left[i],right[j])) {
      SomeSeq[k] <- left[i]
      print(paste0("k:", k, " i:", i, " j:", j , " L:", left[i]," R:", right[j], " Sec:", SomeSeq[k], inequality(left[i],right[j])))
      i <- i+1
    }
    else {
      SomeSeq[k] <- right[j]
      print(paste0("k:", k, " i:", i, " j:", j , " L:", left[i]," R:", right[j], " Sec:", SomeSeq[k], inequality(left[i],right[j])))
      j <- j+1
    }
  }
  SomeSeq
}

Aucun commentaire:

Enregistrer un commentaire