mercredi 19 juin 2019

Logical Operator "OR" producing inconsistent results

I have two datasets Mtest & Ztest. I'm trying to compare each values in the respective datasets to zero and use the result of the comparison to create a new dataset with the results. The comparison should result in one of three outcomes.

Result 1: Mtest > 0 & Ztest > 0 <- "OW"
Result 2: Mtest < 0 & Ztest < 0 <- "UW"
Result 3: Mtest < 0 & Ztest > 0 | Mtest > 0 & Ztest < 0 <- "MW"

Dataset 1: Mtest:

     A       B       C      D       E       F       G        H
1   -0.0306 0.0148  -0.0194 -0.0074 -0.0710 0.0020  -0.0060 -0.0010
2   -0.0082 0.0146  -0.0058 0.0030  -0.0350 0.0073  0.0108  0.0003
3   0.0038  0.0267  -0.0017 0.0186  -0.0051 0.0115  0.0361  0.0027
4   0.0150  0.0304  0.0011  0.0267  0.0261  0.0128  0.0378  0.0078
5   0.0048  0.0421  -0.0140 0.0327  0.0462  0.0141  0.0830  0.0111
6   0.0225  0.0448  -0.0060 0.0369  0.0602  0.0158  0.0711  0.0121

Dataset 2: Ztest

    A       B       C       D       E       F       G       H
1   3.6746  0.3508  3.6308  2.6540  1.6122  3.3419  -0.0075 0.2960
2   1.0267  -0.6883 0.5399  -0.2005 -0.5095 0.1366  -0.6247 -0.0911
3   0.3776  2.2182  0.2944  1.8630  1.6847  1.6018  1.6330  0.3921
4   0.7568  -0.5380 0.3686  0.1086  0.0234  0.1360  -0.7992 0.0055
5   -1.0248 2.6451  -1.5115 0.9517  0.7697  1.1084  2.0410  0.5189
6   1.0474  0.0568  0.7330  0.2214  -0.0264 -0.1067 -0.3753 0.0055

Desired Output:

    A   B   C   D   E   F   G   H
1   MW  OW  MW  MW  MW  OW  UW  MW
2   MW  MW  MW  MW  UW  OW  MW  MW
3   OW  OW  MW  OW  MW  OW  OW  OW
4   OW  MW  OW  OW  OW  OW  OW  OW
5   MW  OW  UW  OW  OW  OW  OW  OW
6   OW  OW  MW  OW  MW  MW  MW  OW

My logic is not generating MW for Column E Row 3. Below is a sample of the output my loop is generating:

    A   B   C   D   E   F   G   H
1   OW  OW  OW  OW  OW  OW  UW  OW
2   OW  MW  OW  MW  UW  OW  MW  MW
3   OW  OW  OW  OW  OW  OW  OW  OW
4   OW  MW  OW  OW  OW  OW  MW  OW
5   MW  OW  UW  OW  OW  OW  OW  OW
6   OW  OW  OW  OW  MW  MW  MW  OW

When I run the following loop, it isn't generating a "MW" result consistently and I think it has to do with the "OR" but can't figure out what is causing this problem since the logic looks correct to me. An example of this is (Column E Row 3) should be "MW", but is coming up with "OW"

    testMatrix <- matrix(0, ncol = 8, nrow = 6)

for(i in 1:nrow(Ztest)) {
  for(j in 1:ncol(Ztest)) {
    if(c(Ztest[i,j], Mtest[i,j]) == "TRUE"){
      testMatrix.tf[i,j] <- "OW"
    } else {
      if(((Ztest[i,j] == "TRUE") & (Mtest[i,j] != "TRUE")) | ((Ztest[i,j] != "TRUE") & (Mtest[i,j] == "TRUE"))){
        testMatrix[i,j] <- "MW"
      } else {
        if(c(Ztest[i,j],Mtest[i,j]) != "TRUE"){
          testMatrix.tf[i,j] <- "UW"
        }
      }
    }
  }
} 

As always, I appreciate any help you can provide me uncovering the issue with my logical comparisons.

Aucun commentaire:

Enregistrer un commentaire