Skip to content

Instantly share code, notes, and snippets.

@mcanouil
Last active February 2, 2024 23:34
Show Gist options
  • Save mcanouil/6e341eeb7cf5af7858257d1d1314f379 to your computer and use it in GitHub Desktop.
Save mcanouil/6e341eeb7cf5af7858257d1d1314f379 to your computer and use it in GitHub Desktop.
HbA1c Percent to mmol/L
# # MIT License
#
# Copyright (c) 2024 Mickaël Canouil
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
hba1c_percent_to_mmoll <- function(x, unitFrom = "%") {
if (unitFrom == "%" & nchar(system.file(package = "broom")) != 0) {
HBA1Cconvert <- structure(list(
V1 = c(
10L, 12L, 14L, 16L, 18L, 20L, 22L, 24L,
26L, 28L, 30L, 32L, 34L, 36L, 38L, 40L, 42L, 44L, 46L, 48L, 50L,
52L, 54L, 56L, 58L, 60L, 62L, 64L, 66L, 68L, 70L, 72L, 74L, 76L,
78L, 80L, 82L, 84L, 86L, 88L, 90L, 92L, 94L, 96L, 98L, 100L,
102L, 104L, 106L, 108L, 110L, 112L, 114L, 116L, 118L, 120L, 122L,
124L, 126L, 128L, 130L, 132L, 134L, 136L, 138L, 140L, 142L, 144L,
146L, 148L, 150L, 152L, 154L, 156L, 158L, 160L, 162L, 164L, 166L,
168L, 170L, 172L, 174L, 176L, 178L, 180L, 182L, 184L, 186L, 188L,
190L, 192L, 194L, 196L, 198L, 200L, 202L, 204L, 208L, 210L
),
V2 = c(
3.1, 3.2, 3.4, 3.6, 3.8, 4, 4.2, 4.3, 4.5, 4.7, 4.9,
5.1, 5.3, 5.4, 5.6, 5.8, 6, 6.2, 6.4, 6.5, 6.7, 6.9, 7.1,
7.3, 7.5, 7.6, 7.8, 8, 8.2, 8.4, 8.6, 8.7, 8.9, 9.1, 9.3,
9.5, 9.7, 9.8, 10, 10.2, 10.4, 10.6, 10.8, 10.9, 11.1, 11.3,
11.5, 11.7, 11.8, 12, 12.2, 12.4, 12.6, 12.8, 12.9, 13.1,
13.3, 13.5, 13.7, 13.9, 14, 14.2, 14.4, 14.6, 14.8, 15, 15.1,
15.3, 15.5, 15.7, 15.9, 16.1, 16.2, 16.4, 16.6, 16.8, 17,
17.2, 17.3, 17.5, 17.7, 17.9, 18.1, 18.3, 18.4, 18.6, 18.8,
19, 19.2, 19.4, 19.5, 19.7, 19.9, 20.1, 20.3, 20.4, 20.6,
20.8, 21, 21.2
)
), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, -100L))
return(sapply(x, function(y) {
round(sum(broom::tidy(lm(V1 ~ V2, data = HBA1Cconvert))[, "estimate"] * c(1, y)))
}))
} else {
return(x)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment