'R code for new CKD equation - with conditional statements
I'm new to coding in R and am having a difficult time coding the following equation
(There is an existing R package for the old version of this equation, but not this updated one).
Could someone help me with the code? Having a hard time figuring out how to create a new variable (eGFR) with values calculated from this equation. The components A and B of the equation depend on 2 categories of variable Scr (serum creatinine) and on gender (M/F). Thanks!
Solution 1:[1]
If you have a conditional mutiplier, the easiest thing to do is set the multiplier to 1 when the condition is not met (since multiplying by 1 is the same as not multiplying at all)
eGFR <- function(Scr, age, sex) {
sex <- tolower(sex)
A <- ifelse(sex == "female", 0.7, 0.9)
B <- ifelse(Scr > 0.7, -1.2, ifelse(sex == "female", -0.241, -0.302))
mult <- ifelse(sex == "female", 1.012, 1)
142 * (Scr/A)^B * 0.9938^age * mult
}
eGFR(0.6, 35, "male")
#> [1] 129.1019
eGFR(0.8, 80, "female")
#> [1] 74.43855
eGFR(Scr = c(0.6, 0.8), age = c(35, 80), sex = c("male", "female"))
#> [1] 129.10190 74.43855
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
