I'm new to R and trying to populate a column bmi_cat
in my data frame dat
based on the numerical value provided in the bmi
column. However, it populates all of bmi_cat
with "Normal" even when that is incorrect. The last bmi
value in the dataframe is within the Normal range, so I suspect it is continuously updating the entirety of bmi_cat
with the most recent result. However, I'm not sure why. Can anyone point out the fault in my approach?
for (num in 1:nrow(dat)){
if (dat$bmi[num] <= 18.5) {
dat$bmi_cat[num] <- "Underweight"
} else if (dat$bmi[num] > 18.5 & dat$bmi[num] <= 25) {
dat$bmi_cat[num] <- "Normal"
} else if (dat$bmi[num] > 25 & dat$bmi[num] < 30) {
dat$bmi_cat[num] <- "Overweight"
} else if (dat$bmi[num] >= 30){
dat$bmi_cat[num] <- "Obese"
}
}
I hope this was enough information. Thank you in advance.