title: Why does sapply() return a list?
body: <p>I faced up a strange behaviour in <a href="http://en.wikipedia.org/wiki/R_%28programming_language%29" rel="noreferrer">R</a> with the <code>sapply()</code> function. This function is supposed to return a vector, but in the special case where you give it an empty vector, it returns a list.</p>
  
  <p>Correct behaviour with a vector:</p>
  
  <pre><code>a = c("A", "B", "C")
  a[a == "B"]  # Returns "B"
  a[sapply(a, function(x) {x == "B"})] # Returns "B"
  </code></pre>
  
  <p>Correct behaviour with a NULL value:</p>
  
  <pre><code>a = NULL
  a[a == "B"]  # Returns NULL
  a[sapply(a, function(x) {x == "B"})] # Returns NULL
  </code></pre>
  
  <p>Strange behaviour with an empty vector:</p>
  
  <pre><code>a = vector()
  a[a == "B"]  # Returns NULL
  a[sapply(a, function(x) {x == "B"})] # Erreur : type 'list' d'indice incorrect
  </code></pre>
  
  <p>Same error message as with this statement:</p>
  
  <pre><code>a[list()] # Erreur dans a[list()] : type 'list' d'indice incorrect
  </code></pre>
  
  <p>Why? Is it a bug?</p>
  
  <p>Due to this strange behaviour, I use <code>unlist(lapply())</code>.</p>