title: In R, how can I check whether a list contains a particular key?
body: <p>Suppose I have a list as follows</p>
  
  <pre><code>foo=list(bar="hello world")
  </code></pre>
  
  <p>I would like to check whether my list has a particular key.
  I observe <code>foo$bar2</code> will return <code>NULL</code> for any <code>bar2</code> that is not equal to <code>bar</code>, so I figured I could check for whether the return value was null, but this does not seem to work:</p>
  
  <pre><code>if (foo$bar2==NULL) 1 # do something here
  </code></pre>
  
  <p>However, this gives the error:</p>
  
  <pre><code>Error in if (foo$bar2 == NULL) 1 : argument is of length zero
  </code></pre>
  
  <p>I then tried whether NULL is equivalent to false, like in C:</p>
  
  <pre><code>if (foo$bar2) 1 # do something here
  </code></pre>
  
  <p>This gives the same error. </p>
  
  <p>I now have two questions. How can I check whether the list contains the key?
  And how do I check whether an expression is null?</p>