Costello Medical interview question

Do this assignment in R