0
votes

I'm new to coding in R and am having a difficult time coding the following equation

CKD EPI 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!

1
Welcome to SO! You are attracting close votes because you have not provided a minimal reproducible example, have demonstrated no attempt to solve your problem on your own and have posted relevant information as an image. These posts may help: MREs and images. - Limey

1 Answers

0
votes

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