One exercise I get students to do in one of my early R Programming classes is write code to calculate Body Mass Index (BMI), and then to use an if statement to determine what the value of the BMI indicates about your health. BMI is a gross estimate for the amount of fat in your body - the categories are as follows:
|Image source: JoyDeepDev.|
BMI is calculated as: BMI = Weight (kg) / Height (m) x Height (m). Before my recent (medically advised) diet, my weight was 104 kgs. My height is 1.82 metres - my BMI is therefore 31.4, which put me firmly into the "Obese" category. This is a term which I would not have referred to myself as although I am heavy - I didn't consider myself "obese".
Here is the R code that I use in class:
# Short Exercise - calculate BMI
weight = as.numeric(readline(prompt="What is your weight (kilos): "))
height = as.numeric(readline(prompt="What is your height (metres): "))
# Calculate BMI
bmi = weight/(height * height)
print(paste("Your weight is", weight,"kg,", "your height is", height, "m,", "and your BMI is", bmi))
The good news for me is that above emphasis on BMI, and my doctor's advice, is that my recent diet has reduced my BMI to 29.9 - out of the "Obese" category, and into the "Overweight" category. To get to "Normal" I would have to lose another 18 kgs, that's 37 lbs or 3 stones!
Post a Comment