Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions R/convert_conc_unit.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,22 @@ convert_conc_unit <- function(value, unit_in, unit_out, mol_weight = NULL) {
`g/l` = 1000000,
mg_dl = 10000,
`mg/dl` = 10000,
mg_l = 1000,
`mg/l` = 1000,
pg_ml = 1e-3,
`pg/ml` = 1e-3,
`micromol/l` = mol_weight,
micromol_l = mol_weight,
micromol = mol_weight,
mmol = mol_weight,
mmol = mol_weight * 1000,
mmol_l = mol_weight * 1000,
`mmol/l` = mol_weight * 1000,
`mumol/l` = mol_weight,
`umol/l` = mol_weight,
mumol_l = mol_weight,
umol_l = mol_weight
umol_l = mol_weight,
pmol_l = mol_weight * 1e-6,
`pmol/l` = mol_weight * 1e-6
)

unit_out <- match.arg(unit_out, names(conv))
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test_convert_conc_unit.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ test_that("convert_conc_unit can convert g/dl, g/l, mg/dl", {
expect_equal(convert_conc_unit(1, "mg_dl", "g_dl")$value, .001)
expect_equal(convert_conc_unit(1, "mg_dl", "g_l")$value, 0.01)
expect_equal(convert_conc_unit(1, "g_l", "mg_dl")$value, 100)
expect_equal(convert_conc_unit(1, "g_l", "mg_l")$value, 1000)
expect_equal(convert_conc_unit(1, "mg_l", "pg_ml")$value, 1e6)
})

test_that("molecular weight required when converting molar units", {
Expand All @@ -17,6 +19,8 @@ test_that("molecular weight required when converting molar units", {

test_that("molar unit conversion works", {
expect_equal(convert_conc_unit(1, "mg_dl", "micromol_l", 0.5)$value, 20000)
expect_equal(convert_conc_unit(200, "pg_ml", "pmol_l", 50)$value, 4000)
expect_equal(convert_conc_unit(1, "mmol_l", "pmol_l", 17)$value, 1e9)
})

test_that("vectorized input works", {
Expand Down