title: Removing white-spaces conditioned on number of occurrences in R
body: <p>I want to remove white-spaces from a character string when the number of continuous white-spaces between <em>2 non-whitespace chars</em> <s>words</s> is less than a certain number. </p>
  
  <p>For example,</p>
  
  <pre><code>a &lt;- c("I want            only                &lt;5                         white-spaces   removed")
  </code></pre>
  
  <p>I know I can remove all the spaces using <code>gsub(' ', '', a)</code>. However, I want to remove white-spaces between <em>two non-whitespace chars</em> <s>words</s> only when the total white-space is less than 5. So I want the following</p>
  
  <pre><code>a_adj &lt;- c("Iwant             only                &lt;5                         white-spacesremoved")
  </code></pre>
  
  <p>I tried this <code>gsub('{,5} ', '', a)</code>. But it still removes all white-spaces. Can someone help, please?</p>
  
  <p>Thanks</p>