title: How to replace the space between single letters with a period
body: <p>I am trying to replace the space between single letters (initials of names) with a period. There should also be a trailing period after the final letter.</p>
  <p>For example:</p>
  <blockquote>
  <p>&quot;h a g wells j g verne&quot; becomes &quot;h.a.g. wells j.g. verne&quot;<br />
  and<br />
  &quot;h a g wells j g&quot;         becomes &quot;h.a.g. wells j.g.&quot;</p>
  </blockquote>
  <p>This question <a href="https://stackoverflow.com/questions/4228750/removing-spaces-between-single-letters">Removing spaces between single letters</a>, has got it most of the way but I am struggling to get the trailing period. Could I have some help please.</p>
  <p>My efforts</p>
  <pre><code>x &lt;- &quot;h a g wells j g verne&quot; 
  y &lt;- &quot;h a g wells j g&quot;       
  
  # Almost but need trailing period as well
  temp &lt;- gsub(&quot;(?&lt;=(?&lt;!\\w)\\w) (?=\\w(?!\\w))&quot;, &quot;.&quot;, x , perl=TRUE) 
  temp
  # [1] &quot;h.a.g wells j.g verne&quot;
   
  # Trying for the trailing periods
  # messes first by matching first letter but okay in second 
  gsub(&quot;(\\.\\w)&quot;, &quot;\\1.&quot;, temp) 
  # [1] &quot;h.a..g. wells j.g. verne&quot;
  
  # messes first by matching first letter but okay in second 
  gsub(&quot;(\\w\\.\\w)&quot;, &quot;\\1.&quot;, temp) 
  # [1] &quot;h.a..g wells j.g. verne&quot;
  
  # messes first by matching first letter but okay in second 
  gsub(&quot;(?&lt;=\\.\\w)&quot;, &quot;.&quot;, temp, perl=TRUE) 
  # [1] &quot;h.a..g. wells j.g. verne&quot;
   
  # Success 
  temp2 &lt;- gsub(&quot;(\\.\\w)\\s&quot;, &quot;\\1. &quot;, temp)
  temp2
  # [1] &quot;h.a.g. wells j.g. verne&quot;
   
  # ... but not for y which misses the second trailing period
  temp &lt;- gsub(&quot;(?&lt;=(?&lt;!\\w)\\w) (?=\\w(?!\\w))&quot;, &quot;.&quot;, y , perl=TRUE) 
  gsub(&quot;(\\.\\w)\\s&quot;, &quot;\\1. &quot;, temp)  
  # [1] &quot;h.a.g. wells j.g&quot;
  </code></pre>