Introduction to Data Analysis

2.3. Practice

Instructions: this week’s exercise is called 2_bmi.R. Download or copy-paste that file into a new R script, then open it and run it with the working directory set as the IDA folder. If you download the script, make sure that your browser preserved its .R file extension.

The exercise for this week is a quick overview of the main R object classes: vectors, matrices, data frames and factors. Each class will be useful to manipulate data in the next sessions, so make sure that you are familiar enough with them. The rest of this page documents the exercise and expands on what you learn from it.

Computing the Body Mass Index

An important aspect of R syntax is assignment. As we saw previously, to put some values into an object, you need two things, separated by a right-left arrow (or by a single equal sign if you prefer). The example below creates an object that holds my Body Mass Index (BMI), and then evaluates if it sits in the “normal” range.

# Compute my Body Mass Index.
bmi <- round(703 * 134/(70^2), 1)
# Create a text object (called a string).
assessment = "normal"
# Modify the assessment statement if BMI is below 18 or above 25.
if (bmi < 18) assessment = "below normal"
if (bmi > 25) assessment = "above normal"
cat("My BMI is approximately", bmi, ", which is", assessment)
My BMI is approximately 19.2 , which is normal

In fact, it would make more sense to write a Body Mass Index function, which is not very difficult if you can survive the additional brackets. We will come back shortly to writing functions next week, but here's a quick example of a BMI function.

# A simple Body Mass Index function.
bmi <- function(weight, height, digits = 2) {
    round(weight * 703/(height^2), digits)
}
# An object called 'bmi' now appears in your Workspace.  Check result.
bmi
function(weight, height, digits = 2) {
    round(weight * 703/(height^2), digits)
}
<environment: 0x7ff00cde7ad8>
# This object is a function.
class(bmi)
[1] "function"
# Example, with default argument of 2 digits.
bmi(weight = 134, height = 70)
[1] 19.22
# Another example, this time with no digits.
bmi(weight = 134, height = 70, digits = 0)
[1] 19

Computing Quételet's BMI function

The exercise shows how to create the bmi.quetelet function that computes the BMI in its original metric units, so that you can compute your own value.

# Quételet's BMI function in kg/m.
bmi.quetelet
function(weight, height, digits = 2) {
  bmi <- weight / (height^2)
  bmi.scale <- c(0, 18.5, 25, 30, 40)
  bmi.class <- c("Underweight", "Normal", "Overweight", "Obese", "Mordibly obese")
  class <- bmi.class[length(bmi.scale[bmi > bmi.scale])]
  # Save results as a vector. Note how we round the BMI only at "print stage".
  r <- c(round(bmi, digits), class)
  # Return BMI and classification.
  return(r)
}
# My current BMI (AFAIK!) in kg/m.
bmi.quetelet(60, 1.75)
[1] "19.59"  "Normal"

Course reminders

Remember that a lot of what we cover here, especially with regards to the basic functioning of R, is also covered in many other tutorials. If you enjoy video tutorials, those by Anthony Damico are short and efficient: try his 2-minute approach to arithmetic in R.

Most importantly, do not forget the textbook readings. Next week, we continue manipulating objects and delve a bit deeper into programming full-fledged functions: reading from textbooks will train you in performing these operations.

Next week: Functions.