Skip to content
Prev 245825 / 398506 Next

Parsing a Simple Chemical Formula

On Sun, Dec 26, 2010 at 7:26 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
Here is a variation that is slightly simpler. The function in the
third argument has been changed from c to paste so that it outputs
strings like "C 5".  With this form of output we can use read.table to
read it directly creating a data frame.
+   "([A-Z][a-z]*)(\\d*)",
+   ~ paste(..1, if (nchar(..2)) ..2 else 1),
+   simplify = ~ read.table(textConnection(..1)))
  V1 V2
1  C  5
2  H 11
3 Br  1
4  O  1