title: R - Looping with while always results in missing value where TRUE/FALSE is expected
body: <p><strong>EDIT:</strong> I implemented offered solutions so far, and the code looks way cleaner now. This was the key to finally finding my error. It was a logical condition that I didn't check within the while loop. It could happen that the iterator would exceed the number of elements in the vector and thus pass a &quot;NA&quot; to the while condition! Thx</p>
  <p>I also changed the solution to use vector assignments to store the results and then recombine after the for loop, as vector indexing seems to be way faster than data.table indexing and value assignment within the loop.</p>
  <hr />
  <p>Pls let me apologize first for any errors and lack of information for troubleshooting my problem as this is my first post so far. I have already read that this can happen accidentally whenever ther is an error in a computation and the value of a condition results in an error, such as
  <code>if (TRUE &amp; sqrt(-1))</code></p>
  <p>It's been days and I am still receiving this error. It really gives me a headache, as the inherent logic behind such code is actually pretty straigth forward and I still can't properly formalize it. It goes like following: Compare for each unique bond ID contained in a vector of size N (loop through with i), the static value of its corresponding maturity to 7 periods' end date for distinct set of rules (loop through with k) to determine which periods with unique rules the respective issue falls into, and then determine by looping through all the periods' size thresholds (loop through by l) to find if a particular issue has violted these minimium size requirements. If a violation is determined, I can assign the date of the violation. If <code>(l == k)</code>, I can reckon that for all periods that the issue's maturity falls into, have also successfully looped through the corresponding size requirements checks and as such hasn't violated any rules. I then assign the result of the conditional checks as corresponding binary values in a new data.table column as well as the violation date. So far, I really cant determine what is casusing this error.</p>
  <p>My data looks like following. I have a pretty large data.table containing bond issue identifiers and various other column variables that describe those issues. It was imported as initially with the <code>read_dta()</code> function and then transformed to a data.table with <code>setDT()</code>.</p>
  <p>I extract 3 columns out of this data.table, using</p>
  <pre><code>issue_IDs.vec     &lt;- as.numeric(issues.dt[[2]])
  maturity.vec      &lt;- as.Date(issues.dt[[8]], &quot;%Y-%m-%d&quot;)
  offerings_atm.vec &lt;- as.numeric(issues.dt[[33]])
  </code></pre>
  <p>Next, I transform eligibility criteria of an index as following.</p>
  <pre><code># (1) Creating size requirement end periods (valid thru) ----
  size_req_per_1 &lt;- as.Date(&quot;1992-01-01&quot;, &quot;%Y-%m-%d&quot;)
  size_req_per_2 &lt;- as.Date(&quot;1994-01-01&quot;, &quot;%Y-%m-%d&quot;)
  size_req_per_3 &lt;- as.Date(&quot;1999-07-01&quot;, &quot;%Y-%m-%d&quot;)
  size_req_per_4 &lt;- as.Date(&quot;2003-10-01&quot;, &quot;%Y-%m-%d&quot;)
  size_req_per_5 &lt;- as.Date(&quot;2004-07-01&quot;, &quot;%Y-%m-%d&quot;)
  size_req_per_6 &lt;- as.Date(&quot;2017-02-01&quot;, &quot;%Y-%m-%d&quot;)
  size_req_per_7 &lt;- as.Date(&quot;2021-02-01&quot;, &quot;%Y-%m-%d&quot;)
  size_req_val_per.vec &lt;- c(size_req_per_1, size_req_per_2, size_req_per_3, size_req_per_4,
                            size_req_per_5, size_req_per_6, size_req_per_7)
  
  # (2) Create a size requirement threshold per rules' validity period ----
  size_req_thresh_1   &lt;-  25000
  size_req_thresh_2   &lt;-  50000
  size_req_thresh_3   &lt;- 100000
  size_req_thresh_4   &lt;- 150000
  size_req_thresh_5   &lt;- 200000
  size_req_thresh_6   &lt;- 250000
  size_req_thresh_7   &lt;- 300000
  size_req_thresh.vec &lt;- c(size_req_thresh_1, size_req_thresh_2, size_req_thresh_3,
                           size_req_thresh_4, size_req_thresh_5, size_req_thresh_6,
                           size_req_thresh_7)
  </code></pre>
  <p>Next, I do write a loop to perform conditional checks to find for each issue ID stored in the issues_ID.vec if they violate the index eligibility criterium of the minimim issance size during their maturity. I do this by passing the value of iterator variable i as a position value to the issues_ID.vec.</p>
  <pre><code># (3) Looping through a set of conditional check to find out if and if so when a particular issue violated the size requirement ---
     
  # Iterator variables ----
  # Length of issues.dt
  j &lt;- issues.dt[, .N]
  
  # Main iterator looping through all entries of isssues.dt extracted as vector
  i &lt;- 1
  
  # Looping through vector elements of issue rules (vec. 1: validity periods)
  k &lt;- 1
  
  # Looping through vector elements of issue rules (vec. 2: size thresholds) 
  l &lt;- 1
  
  
  
  # Loop
  
  for (i in 1:j) {
    id            &lt;- issue_IDs.vec[i]
    maturity      &lt;- maturity.vec[i]
    offering_atm  &lt;- issue_IDs.vec[i]
    
    k &lt;- 1
    maturity_comp &lt;- size_req_val_per.vec[k]
    
    while (maturity &gt;= maturity_comp) {
      if (k &lt; 7) {
        k &lt;- k + 1
        maturity_comp &lt;- size_req_val_per.vec[k]
      } else {
        break
      }
    }
    
    l &lt;- 1
    offering_size_comp &lt;- size_req_thresh.vec[l]
    
    for (l in 1:k) {
      if (offering_atm &gt;= offering_size_comp) {
        offering_size_comp &lt;- size_req_thresh.vec[l]
        next
      } else {}
    }
    
    if (l == k) {
      issues.dt[ISSUE_ID == id,
                `:=`(SIZE_REQ_VIOLATION = 0,
                     SIZE_REQ_VIOLATION_DATE = NA)]
    } else {
      issues.dt[ISSUE_ID == id,
                `:=`(SIZE_REQ_VIOLATION = 1,
                     SIZE_REQ_VIOLATION_DATE = size_req_val_per.vec[l])]
    }
    i &lt;- i + 1
  }
  </code></pre>
  <p>Whenever I try running the code in a simplified version, such as</p>
  <pre><code>k &lt;- 1
  for (1 in 1:7) {
    print(maturity &gt;= maturity_comp)
    k &lt;- k + 1
    maturity_comp &lt;- format(as.Date(size_req_val_per.vec[k]), &quot;%Y-%m-%d&quot;)
  }
  </code></pre>
  <p>the code runs smooth and always results in the printed evaluations TRUE or FALSE, depending which ID I initially to create the corresponding static maturity of the particular bond issue. As this stage, I already exhasuted my troubleshooting skills.</p>
  <p>I'd appreciate any input from you guys, and if you need any additional information, explanations etc. just let me know.</p>