title: convert math equations into code with ifelse and min/max in R Studio
body: <p>For a medical study I would like to calculate the eGFR, a measure of renal function, with an equation that require certain input values: Scr (serum creatinine), ScysC (serum cystatin C), age and sex-depending values, which are all available in my dataset. </p>
  
  <p>Please see the attached image for the equations. <a href="https://i.stack.imgur.com/Jtrv9.png" rel="nofollow noreferrer">eGFR equation</a></p>
  
  <p>So I am mainly struggling with ifelse-statements and the min/max numbers. How do I create a code to retrieve the output with this equation?</p>
  
  <p>My first thought is to create a loop function, but I don't know exactly how. So any help and time is very much appreciated :)</p>
  
  <p>-EDIT-
  NOTICE: it is important that the ratio between min/max is always &lt;1. 
  e.g. a female with Scr= 0.9 gives Scr/k= 0.9/0.7=1.28 and results in min=1 and max=1.28. 
  A female with Scr=0.6 gives Scr/k= 0.6/0.7=0.86 and results in min=0.86 and max=1.</p>
  
  <p>Here is a sample of my data:</p>
  
  <pre><code>df &lt;- 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 and k=0.9 if M, a=-0.248 if F and a=-0.207 if M)
  </code></pre>