BMI computation and classification using R #14
Replies: 1 comment
-
@OxfordIHTM/class-2024 see @jillianfrancise's function that classifies BMI! Thank you for sharing this @jillianfrancise! If you notice, I just made some formatting changes to your text so that the code elements of the post will show as code. This is done through Markdown syntax right on the comment bubble. I will teach the class how to make this formatting in our last session for Michaelmas term. For everyone, this function will be useful for us during our next lesson with the data on weight and height for adolescent kids (greater than 10 years old). So, please take note. We will need something similar to this but tailored for classifying BMI for children above 10 years old. |
Beta Was this translation helpful? Give feedback.
-
NOTES
obligatory disclaimer: this is the first time I've coded anything independently since literally 2009, when if not the majority then at least a plurality of you were in elementary school, so take everything I say with a handful of salt. I also don't know what I'm doing.
the BMI calculation is fairly straightforward, so I felt like figuring out how to make if-else statements work!
if you're not familiar with coding, an if-else statement is a bit of code that tells R to do one thing if a condition is met, and another thing if the condition isn't! so I just plugged in the BMI cutoff values and asked R to print a statement telling you what category your BMI falls into once you've entered your weight and height
SO. the line
means that if the BMI as calculated earlier is greater than or equal to 25 but less than 30, the program will spit out a comment saying that you're overweight. etc.
this originally existed as two functions, one just for calculating BMI and one for doing the classification; Ernest pointed out that the BMI calculating bit could exist inside the BMI classifying function
had so much trouble during my first iteration of this because it wasn't doing anything when the BMI was normal; realized I forgot to add a catch-all
else
condition (else{print("congratulations! your bmi is normal! :)")}
) after all the ifs. if you don't tell the program what to do when none of theif
conditions have been met it just won't do anything. don't be like me, don't do that.also had a lot of trouble with the brackets- if you make a bracket
{
you have to close it}
and make sure they all match up in the right places, or else the entire thing won't workanyway, feel free to try this out- run it, type in
classify_bmi([whatever weight you want to input], [whatever height you want to input])
and it should do its thing! (don't type in the brackets [], just the numbers).it should also work if you input
wt=[whatever weight you want to input]
, thenht=[whatever height you want to input]
, thenclassify_bmi(wt, ht)
also feel free to add a severely underweight classification lol.
Beta Was this translation helpful? Give feedback.
All reactions