Parses a character vector of molecular formulas and returns a data.table where each row represents
a molecular formula, and each column corresponds to an element, showing the count of atoms of that element.
The resulting table follows the Hill system order for element arrangement.
convert_molecular_formula_to_data_table(
mf,
masses = ume::masses,
table_format = c("wide", "long")
)A data.table with:
Standardized molecular formula following the Hill order.
Original input molecular formula.
Exact molecular mass calculated from element masses.
Columns for each element present in the formulas, showing the atom count.
Character vector of molecular formula(s)
(e.g., c("C10H23NO4", "C10H24N4O2S")).
A data.table. Defaults to ume::masses (based on NIST data)
containing isotope information for elements, including nominal and exact mass,
relative abundance, and Hill system order.
A string (two options) that controls the output table format: wide (DEFAULT, dcast) or long (normalized, melt)
If duplicate formulas are detected, only unique ones are processed, and a warning is issued.
If invalid element symbols are found, the function stops with an error message.
If a molecular formula contains duplicate isotopes/elements, an error is triggered.
The function correctly handles isotopic notations such as [13C] and [18O2].
The output follows the Hill order, meaning C, H first, followed by other elements in alphabetical order.
Single-element counts (e.g., C1H4 → CH4) are formatted without explicit 1.
This function extracts element counts from molecular formulas, including those with isotopic notation.
It ensures that only valid elements are included based on a reference table (masses) and flags invalid entries.
Duplicate molecular formulas are identified and processed only once, with a warning issued.
The function internally creates an enriched masses table to account for isotopic symbols and standard element notation.
Other molecular formula functions:
convert_data_table_to_molecular_formulas()
# Example usage
molecular_formulas <- c("C10H23NO4", "C10H24N4O2S", "C6[13C2]H12[18O2]ONaCl")
convert_molecular_formula_to_data_table(molecular_formulas)
Run the code above in your browser using DataLab