title: Using regular expressions to remove space between stand-alone characters in R
body: <p>I have a vector of characters in R and I want to use regular expressions to pull out a certain pattern. I would like to find occurrences where there are two or more stand-alone letters in a row. This can occur either within a longer string or at the beginning of the longer string. I would then like to remove the space between these letters. I want to keep the space if it does not occur between two stand-alone letters. Below are a couple examples of what I am looking for.</p>
  
  <p>If the vector was:</p>
  
  <pre><code>v &lt;- c("A B C", "ABC D E", "A BC D E FH", "A B CDE")
  </code></pre>
  
  <p>"A B C" should become "ABC"</p>
  
  <p>"ABC D E" should become "ABC DE"</p>
  
  <p>"A BC D E FH" should become "A BC DE FH"</p>
  
  <p>"A B CDE" should become "AB CDE"</p>
  
  <p>So that the resulting vector after I remove the spacing is:</p>
  
  <pre><code>c("ABC", "ABC DE", "A BC DE FH", "AB CDE")
  </code></pre>
  
  <p>There is no punctuation in the vector, and all the letters are capitalized.</p>
  
  <p>What I have done to locate them already is:</p>
  
  <pre><code>grep("^[A-Z] [A-Z] ", v)
  grep(" [A-Z] [A-Z] ", v)
  </code></pre>
  
  <p>However, I would like to do this in one step if possible and take into account that a string might end with a stand-alone character. I am not sure how to use <code>gsub</code>to delete only the spaces between the characters.</p>