title: R regex: issues with character vectors containing NAs
body: <p>I was trying to collapse all multiple (2 or more) whitespace characters within elements of a vector into a single one, using <code>gsub()</code>, e.g.:</p>
  
  <pre><code>x1 &lt;- c("  abc", "a b c    ", "a  b c")
  gsub("\\s{2,}", " ", x1)
  [1] " abc"   "a b c " "a b c"
  </code></pre>
  
  <p>But as soon as the vector contains <code>NA</code> the substitution fails:</p>
  
  <pre><code>x2 &lt;- c(NA, "  abc", "a b c    ", "a  b c")
  gsub("\\s{2,}", " ", x2)
  [1] NA  " " " " " "
  </code></pre>
  
  <p>However, it works fine if one uses Perl-like regular expressions:</p>
  
  <pre><code>gsub("\\s{2,}", " ", x2, perl = TRUE)
  [1] NA       " abc"   "a b c " "a b c"
  </code></pre>
  
  <p>Does anyone have suggestions as to why R's own regular expressions behave in that way? I'm using R 3.1.1 on Linux x86-64 if that helps.</p>