Skip to content
Prev 245822 / 398506 Next

Parsing a Simple Chemical Formula

On Dec 26, 2010, at 8:28 PM, Bryan Hanson wrote:

            
Using the thermo list that the makeup function accesses to get its  
valid atomic symbols one can arrive at the the answer you posited  
would be too difficult in you first posting, the atomic weight from  
the formulae:

 > str(thermo$element)
'data.frame':	130 obs. of  6 variables:
  $ element: chr  "Z" "O" "H" "He" ...
  $ state  : chr  "aq" "gas" "gas" "gas" ...
  $ source : chr  "CWM89" "CWM89" "CWM89" "CWM89" ...
  $ mass   : num  0 16 1.01 4 20.18 ...
  $ s      : num  -15.6 49 31.2 30.2 35 ...
  $ n      : int  1 2 2 1 1 1 1 1 2 2 ...

patts <- paste("^", rownames(makeup(form)), "$", sep="")
makuform<- makeup(form)
makuform$amass <- sapply(patts, function(x) {return( thermo 
$element[ grep(x, thermo$element[[1]])[1], "mass"])}  )
sum(makuform$amass *makuform$count)
# [1] 167.0457
David Winsemius, MD
West Hartford, CT