R: Function for calculating BMI

  • Print

When learning how to build R functions it is useful to start simple. It is possible to calculate body mass index (BMI) simply in excel, but this is research in R not excel. Though simple enough to build, the BMI function is probably my most used.

BMI_function <- function(weight_kg, height_cm) {
                  return((weight / (height * height)) * 10000)
                }

Then simply calculate BMI by applying the function to your data:

mydata$bmi <- BMI_function(mydata$weight_kg, mydata$height_cm)