Thanks in advance to the community for the wealth of knowledge you all provide.
I wrote the below function to calculate a point total for men as done in the Framingham Heart Study (total points used to determine a 10 yr risk score). What I'm trying to do now is apply this function to a data frame with a set of test patients to get the point total for each individual (i.e. by row). I'm assuming I need a for loop as I've tried apply(), mapply(), etc. and I'm not having any luck with that approach. Any thoughts on how to get the function to pull in the individual values for each attribute into the function by patient? I'm new to coding so I apologize if this is a very ignorant question (I tried searching with very limited success).
risk_men <- function(total_cholesterol, age, taking_bp_medication, systolic_bp, smoke, diabetes, hdl_cholesterol)
{
tot_points = 0
# Adding points based off of total cholesterol
if(total_cholesterol < 160) tot_points = tot_points + 0
else if(total_cholesterol >= 160 & total_cholesterol < 199) tot_points = tot_points + 1
else if(total_cholesterol >= 200 & total_cholesterol < 239) tot_points = tot_points + 2
else if(total_cholesterol >= 240 & total_cholesterol < 279) tot_points = tot_points + 3
else if(total_cholesterol >= 280) tot_points = tot_points + 4
# Adding or subtracting points based off of age
if(age >= 30 & age < 34) tot_points = tot_points + 0
else if(age >= 35 & age < 39) tot_points = tot_points + 2
else if(age >= 40 & age < 44) tot_points = tot_points + 5
else if(age >= 45 & age < 49) tot_points = tot_points + 6
else if(age >= 50 & age < 54) tot_points = tot_points + 8
else if(age >= 55 & age < 59) tot_points = tot_points + 10
else if(age >= 60 & age < 64) tot_points = tot_points + 11
else if(age >= 65 & age < 69) tot_points = tot_points + 12
else if(age >= 70 & age < 74) tot_points = tot_points + 14
else if(age >= 75) tot_points = tot_points + 15
# Adding points for systolic blood pressure stratified by whether or not pt on bp meds
if(taking_bp_medication == 0)
{
if(systolic_bp < 120) tot_points = tot_points - 2
else if(systolic_bp >= 120 & systolic_bp < 129) tot_points = tot_points + 0
else if(systolic_bp >= 130 & systolic_bp < 139) tot_points = tot_points + 1
else if(systolic_bp >= 140 & systolic_bp < 159) tot_points = tot_points + 2
else if(systolic_bp >= 160) tot_points = tot_points + 3
} else if(taking_bp_medication == 1)
{
if(systolic_bp < 120) tot_points = tot_points + 0
else if(systolic_bp >= 120 & systolic_bp < 129) tot_points = tot_points + 2
else if(systolic_bp >= 130 & systolic_bp < 139) tot_points = tot_points + 3
else if(systolic_bp >= 140 & systolic_bp < 159) tot_points = tot_points + 4
else if(systolic_bp >= 160) tot_points = tot_points + 5
}
# Adding points for smoking
if(smoke == 0) tot_points = tot_points + 0
else if(smoke == 1) tot_points = tot_points + 4
# Adding points for diabetes
if(diabetes == 0) tot_points = tot_points + 0
else if(diabetes == 1) tot_points = tot_points + 3
# Adding or subtracting points based on HDL cholesterol levels
if(hdl_cholesterol >= 60) tot_points = tot_points - 2
else if(hdl_cholesterol >= 50 & hdl_cholesterol < 59) tot_points = tot_points - 1
else if(hdl_cholesterol >= 45 & hdl_cholesterol < 49) tot_points = tot_points + 0
else if(hdl_cholesterol >= 35 & hdl_cholesterol < 44) tot_points = tot_points + 1
else if(hdl_cholesterol < 35) tot_points = tot_points + 2
return(tot_points)
}
The patients data frame with the test patients is shown here:
id | sex | total_cholesterol | age | systolic_bp | diastolic_bp | smoke | bmi | diabetes | taking_bp_medication | hdl_cholesterol | ldl_cholesterol |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | 2 | 323 | 39 | 131.5 | 85 | 1 | 24.79 | 0 | 0 | NA | NA |
2 | 1 | 264 | 49 | 127.5 | 81 | 0 | 25.16 | 0 | 0 | 68 | 152 |
3 | 1 | 200 | 57 | 117.5 | 80 | 0 | 25.41 | 0 | 0 | NA | NA |
4 | 1 | 260 | 41 | 137.5 | 80 | 1 | 26.89 | 0 | 0 | NA | NA |
5 | 2 | 312 | 62 | 162.5 | 93.5 | 0 | 25.33 | 0 | 1 | NA | NA |
6 | 1 | 260 | 41 | 120 | 72.5 | 1 | 26.36 | 0 | 0 | 46 | 221 |