title: argument is of length zero - R
body: <p>I have seen many questions on SO with <a href="https://stackoverflow.com/questions/12307139/error-in-if-while-condition-argument-is-of-length-zero">this problem</a> , but I can't understand , and relate to those with my case. I need to test for a case and then take appropriate action. Clearly the following code is not what I would eventually use ... but it illustrates the problem.</p>
  
  <h1>Code</h1>
  
  <h2>Init Code</h2>
  
  <pre><code>threshold &lt;- 10
  slotDuration &lt;- 10 # data arriving in slotDuration sec will get into a single slot
  totalSlots &lt;- 200 # number of slots to keep 
  prevSlot &lt;- 0
  
  window &lt;- slotDuration * totalSlots # window [time in sec] of data to cover
  data &lt;- array (0, dim=c(1,totalSlots))
  handler &lt;- function(transactionTime, dataValue){
      # This line perhaps does not matter as the test uses the destSlot variable below.
      dput(transactionTime)
      n &lt;- as.numeric(transactionTime, units = "secs")
  
      # This is the var that causes problem
     destSlot &lt;- ceiling((n %% window)/slotDuration)
  
     # this line should ensure we dont proceed on null values .. right ?
     if (is.null(destSlot)) {
        print("This is NULLL")
        return (0)
     }
  
     if (!is.null(destSlot)) {
         print(destSlot)       ## prints numeric(0)
         print("This is NOT NULLL")
     }
  
     if (destSlot != 0){ ## This line causes argument length zero error
         prevSlot &lt;&lt;- destSlot
         data[destSlot] &lt;&lt;- 0 
     }
     data[destSlot] &lt;&lt;- data[destSlot] + dataValue
  </code></pre>
  
  <p>}</p>
  
  <h1>Output</h1>
  
  <pre><code>structure(1475591155, class = c("POSIXct", "POSIXt"))
  [1] 116
  [1] "This is NOT NULLL"
  Error in eval(substitute(expr), envir, enclos) : 
  argument is of length zero
  </code></pre>