In this dataset, the units of the height measurement is in centimetres.
Using what we have learned earlier on calculating BMI using R, I can perform the following R commands to get the BMI for each child in each of the schools:
## Calculate BMI for children in school 1516school1516$weight / (school1516$height /100) ^2## Calculate BMI for children in school 1516school1522$weight / (school1516$height /100) ^2## Calculate BMI for children in school 1516school1525$weight / (school1516$height /100) ^2
Because the commands are repetitive, I can easily copy and paste my initial line of code to calculate BMI for children in school 1516 and then just change the object names accordingly to calculate the BMI for children in the two other schools.
When I run these lines of code, I get the following results:
The calculation for the BMI of children in school 1516 seems to have completed without issues and a vector of BMI results have been produced. However, for school 1522 and school 1525, there is a warning saying:
## Warning in school1522$weight/(school1516$height)^2: longer object length is not a multiple## of shorter object length
Although a result has been provided, the warning gives me an indication that someting is not quite right with my calculation and when I inspect further, I notice that in my formula for school 1522 and for school 1525, my denominator is still using data for school 1516 and this is most likely what is causing the warning message.
So, to correct this I go back to my lines of code and edit the denominators for school 1522 and school 1525 as follows:
## Calculate BMI for children in school 1516school1516$weight / (school1516$height /100) ^2## Calculate BMI for children in school 1516school1522$weight / (school1522$height /100) ^2## Calculate BMI for children in school 1516school1525$weight / (school1525$height /100) ^2
I now do not get the warning message and the expected length of BMI values for each school has now been produced.
From this short example above, we realise how tedious a task it is to type in the code above every time we need to calculate BMI. Also, it becomes even challenging to debug issues with the code because we have to review and edit (as needed) each iteration of the calculation to see where it may have gone wrong (especially when doing a cut and paste approach).
It would be better (and easier) to have a function that calculates and displays the BMI values automatically. Fortunately, R allows us to do just that.
The function() function allows us to create new functions in R with the following generic syntax:
function_name <-function(argument1, argument2, ...) {## Your code here}
Using this template/generic syntax, we apply it to create a function called calculate_bmi as follows:
In our example here, the calculate_bmi() function helped a little bit in making the code to calculate BMI for each student in each school more efficient. But the efficiency that functions provide become more evident when you need to make more complex operations. For example, what if you need to get the mean BMI for students in each school? Without a function, we will have to do the following script for each school:
School 1516
## Calculate BMI for children in school 1516bmi_school1516 <- school1516$weight / (school1516$height /100) ^2## Get the mean BMI for children in school 1516mean_bmi_school1516 <-mean(bmi_school1516)mean_bmi_school1516
[1] 16.28491
School 1522
## Calculate BMI for children in school 1522bmi_school1522 <- school1522$weight / (school1522$height /100) ^2## Get the mean BMI for children in school 1522mean_bmi_school1522 <-mean(bmi_school1522)mean_bmi_school1522
[1] 16.89955
School 1525
## Calculate BMI for children in school 1525bmi_school1525 <- school1525$weight / (school1525$height) ^2## Get the mean BMI for children in school 1525mean_bmi_school1525 <-mean(bmi_school1525)mean_bmi_school1525
[1] 0.001564695
As the operations/calculations we want to perform become more complex, the copy and paste method becomes more and more tedious. With the function approach, we can use the following: