title: Extract numbers from Chemical Formula in R
body: <p>My data set (MSdata) looks something like this</p>
  
  <pre><code>m.z       Intensity Relative    Delta..ppm. RDB.equiv.  Composition 
  301.14093   NA       100.00         -0.34   5.5         C16 H22 O4 Na
  149.02331   4083458.5   23.60       -0.08   6.5         C8 H5 O3
  279.15908   NA        18.64         -0.03   5.5         C16 H23 O4
  </code></pre>
  
  <p>and I would like it to look like</p>
  
  <pre><code>m.z       Intensity Relative    Delta..ppm. RDB.equiv.  C    H   O   Na
  301.14093   NA       100.00         -0.34   5.5         16   22  4   1
  149.02331   4083458.5   23.60       -0.08   6.5         8    5   3   0
  279.15908   NA        18.64         -0.03   5.5         16   23  4   0
  </code></pre>
  
  <p>I have gotten as far as using this</p>
  
  <pre><code>library(stringr)
  numextract &lt;- function(string){
  unlist(regmatches(string, gregexpr("[[:digit:]]+\\.*[[:digit:]]*"
                                    ,string)))
  }
  MScomp &lt;- numextract("C14 H18 O4 Na")
  </code></pre>
  
  <p>However, this gives me</p>
  
  <pre><code>'14' '18' '4'
  </code></pre>
  
  <p>I need the 'Na' string to give me a value of 1 or 0 (or NA). I'm new to coding and a lot of this is beyond me- I have been using this <a href="http://stla.github.io/stlapblog/posts/Numextract.html" rel="nofollow noreferrer">website to help me</a>. Additionally I have no idea how to merge these new columns (if this works..) into my current matrix. The website I linked previously uses a newcol() function? Thanks for any help you might have to offer!</p>