Learn R Programming

cliot (version 1.0.0)

convert_lab_units: Convert Common Medical Laboratory Measures Between US and SI Units

Description

Converts measurements for common medical laboratory analytes (e.g., Glucose, Creatinine, Hemoglobin) between the US Conventional unit system (US) and the International System of Units (SI). The function includes a comprehensive lookup table of conversion factors and robust error checking.

Usage

convert_lab_units(value, analyte, from_unit, to_unit)

Value

If successful, returns a character vector of length two: c(converted_value, target_unit_string). If an input error occurs, returns a character string containing a detailed error message.

Arguments

value

A single numeric value of the measurement to be converted. Must be finite and non-missing.

analyte

A single character string specifying the substance being measured (e.g., "Glucose", "Creatinine"). Case-insensitive.

from_unit

A single character string specifying the starting unit system: "US" (US Conventional) or "SI" (International System). Case-insensitive.

to_unit

A single character string specifying the target unit system: "US" or "SI". Case-insensitive.

Details

The function uses a fixed set of multiplication factors (\(F\)) where \(US \times F = SI\).

Supported Analytes and Unit Conversions:

  • Glucose: mg/dL (US) mmol/L (SI). Factor: 0.0555

  • Creatinine: mg/dL (US) umol/L (SI). Factor: 88.4

  • BUN (Urea Nitrogen): mg/dL (US) mmol/L (SI). Factor: 0.357

  • Cholesterol: mg/dL (US) mmol/L (SI). Factor: 0.0259

  • Triglycerides: mg/dL (US) mmol/L (SI). Factor: 0.0113

  • Calcium: mg/dL (US) mmol/L (SI). Factor: 0.25

  • Magnesium: mg/dL (US) mmol/L (SI). Factor: 0.4114

  • Bilirubin: mg/dL (US) umol/L (SI). Factor: 17.1

  • Sodium: mEq/L (US) mmol/L (SI). Factor: 1.0

  • Potassium: mEq/L (US) mmol/L (SI). Factor: 1.0

  • Hemoglobin: g/dL (US) g/L (SI). Factor: 10.0

  • Albumin: g/dL (US) g/L (SI). Factor: 10.0

  • Total Protein: g/dL (US) g/L (SI). Factor: 10.0

  • Iron: ug/dL (US) umol/L (SI). Factor: 0.1791

  • Ferritin: ng/mL (US) pmol/L (SI). Factor: 2.247

Examples

Run this code

#1. Convert Glucose from US (mg/dL) to SI (mmol/L)

us_glucose <- 150
convert_lab_units(us_glucose, "Glucose", "US", "SI")

#2. Convert Creatinine from SI (umol/L) to US (mg/dL)

si_creatinine <- 110
convert_lab_units(si_creatinine, "Creatinine", "SI", "US")

#3. Handling analytes with a factor of 1 (Sodium)

convert_lab_units(140, "Sodium", "US", "SI")

#4. Error Case: Unsupported analyte

convert_lab_units(5, "TSH", "US", "SI")

Run the code above in your browser using DataLab