Exercise 4A: Scripting in R - Conditions and For-loops

In this exercise you will practice your scripting in R.

Getting started

Load libraries and the joined diabetes data set.

If-else statements

In these exercises we don’t use the dataframe yet, that comes later when we have loops. For this part, just declare variables to test your statements, e.g. bp <- 120.

  1. Write an if-else statement that prints whether a person has high (more than 100), low (lower than 50) or normal blood pressure (between 50 and 100).

  2. Write an if-else statement that assigns people high, moderate or low health risk based on their smoking habits (variable Smoker) and BMI:

  • Smoker and BMI greater than 35 -> high risk

  • Smoker or BMI greater than 35 -> moderate risk

  • otherwise low risk

And Smoker should be one of “Smoker”, “Former”, “Never”, “Unknown”.

Verify that your statement works for different combinations of smoking habits and BMI.

Loops

  1. Create a vector with at least five elements of your choice. Use a for loop to print each element individually.

  2. Print each column name in the diabetes_glucose data frame using a for loop.

  3. Loop over all rows of diabetes_glucose and determine whether the person’s blood pressure is high, low or normal with the same conditions as in 1. Print the blood pressure value as well as the statement so you can verify whether you have classified the blood pressure correctly as high, normal or low.

  4. Loop over all rows of diabetes_glucose and extract the smoking habits and BMI for each row and determine the health risk with the same conditions as in Exercise 4.2. Print the smoking habits and BMI as well as the health risk level to make it easier to see whether your code works correctly.

Extract value for i’th row in specific column: df$col1[i]

An easy way to printing several variables is to pass a vector into print: print(c(this, and_that, and_this_too))

  1. Do the same as above but instead of printing the risk status, append it to a list. Start by initiating an empty list.
# Initiate list
risk_status <- list()
  1. Check the length of the list. Is it as expected?

Since we looped through all the rows in the diabetes_glucose dataframe, the list should be as long as there are row in the dataframe.

  1. Add the list as a new column in the diabetes_glucose data frame. Note: Before assigning it, use the unlist() function to convert the list to a flat vector. This ensures that each value aligns correctly with the rows of the data frame.

  2. Make a list of all the column names in diabetes_glucose that contain categorical variables. Write a for loop that goes through the list and prints a barplot for each of the categorical variables.

  3. Make a list of all the column names in diabetes_glucose that contain numeric variables. Make a for loop that goes through the list and prints a boxplot for each of the categorical variables.