if statement - convert math equations into code with ifelse and min/max in R Studio -


for medical study calculate egfr, measure of renal function, equation require input values: scr (serum creatinine), scysc (serum cystatin c), age , sex-depending values, available in dataset.

please see attached image equations. egfr equation

so struggling ifelse-statements , min/max numbers. how create code retrieve output equation?

my first thought create loop function, don't know how. , time appreciated :)

-edit- notice: important ratio between min/max <1. e.g. female scr= 0.9 gives scr/k= 0.9/0.7=1.28 , results in min=1 , max=1.28. female scr=0.6 gives scr/k= 0.6/0.7=0.86 , results in min=0.86 , max=1.

here sample of data:

df <- data.frame(id = c(1,2,3), age = c(36,36, 36),      cysc = c(0.757, 1.34, 1.34), scr = c(0.58, 0.68, 0.68), sex = c(1,1,0))          #male = 1, female 0  #equation: egfr = 135*((min(scr/k,1)**a))*((max(scr/k,1)**-0.601))*(min(scysc/0.8,1)**-0.375)* (max(scysc/0.8,1)**-0.711) * (0.995**age) (*0.969 if female)  (with k=0.7 if f , k=0.9 if m, a=-0.248 if f , a=-0.207 if m) 

ok, i'm guessing structure of data.frame. provided how created mine test since there seem more numbers row.names. assumed 1 male , 0 female. finally, added third female patient test, same clinical results male #2.

df <- data.frame(id = c(1,2,3), age = c(36,36, 36), cysc = c(51.614, 47.669, 47.669), scr = c(0.75776, 1.34, 1.34), sex = c(1,1,0))  male.idx <- df$sex == 1  k <- rep(0.7, nrow(df)) k[male.idx] <- 0.9  <- rep(-0.248, nrow(df)) a[male.idx] <- -0.207  egfr <- 135*pmin(df$scr/k,1)**a*((pmax(df$scr/k,1)**-0.601))*(pmin(df$cysc/0.8,1)**-0.375)*      (pmax(df$cysc/0.8,1)**-0.711) * ifelse(male.idx, 0.995, 0.969)**df$age 

[edited more accurate answer]


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -