title: Idiomatic R to set "not found" data.table values to 0 or FALSE?
body: <p>I have a data.table that is the result of "counting" the number of matching rows in another table:</p>
  
  <pre><code>  set.seed(1)
    numRows = 100
    numTypes = 10
    dt = data.table(type=sample(numTypes, numRows, replace=T),
                    value=rbinom(numRows, 1, .5))
    counts = dt[, .N, by=.(type, value)]
  </code></pre>
  
  <p>I'd like to be able to do simple operations based on the returned value, but R returns integer(0) rather than 0 for non-matching rows:</p>
  
  <pre><code>c = counts[type==3 &amp; value==1, N] 
  print(c)
  # [1] 7
  c = counts[type==11 &amp; value==1, N] 
  print(c)
  # integer(0)
  </code></pre>
  
  <p>This is well documented, but a problem when I try to use the result in a conditional:</p>
  
  <pre><code>if (c &gt; 10) print("greater than 10")  # fails
  # Error in if (c &gt; 10) print("greater than 10") : 
  #  argument is of length zero
  </code></pre>
  
  <p>I'm looking for an idiomatic way to convert the integer(0) that is returned for a non-existent count to a more tractable 0.  This is about the best I have: </p>
  
  <pre><code>c = counts[type==11, value==1]  # integer(0)
  if (length(c) == 0) c = 0       # real 0
  if (c &gt; 10) print("greater than 10")  # no error
  </code></pre>
  
  <p>Obviously I could wrap it in a function, or do it on a single line, but it seems likely I'm missing builtin way to do it.  And while at it, it would be nice to handle NULL and NA (and any other non-numbers) as 0 too.   Is there a good general way to do this?   </p>
  
  <p>I'd like to avoid re-evaluating the lookup or retyping the expression.  Some languages use a short-circuit-or like this:</p>
  
  <pre><code>c = counts[type==1, value==1] || 0  # does not work in R
  </code></pre>
  
  <p>But I haven't found an R equivalent.  I could write my own function, but I'm not sure about performance or readability:</p>
  
  <pre><code>orZero = function(n) if (length(n) == 0 || is.na(n) || is.null(n)) 0 else n  
  </code></pre>
  
  <p>Maybe there is a variant of <code>if</code> that treats integer(0) as FALSE?  Or perhaps there is an argument that can be passed to [.data.table() that will give me a 0 directly? I thought briefly that 'nomatch' might do it, but this seems to be different and used only for joins. </p>