Course Notes Home

Exercise 1

Write an if else statement that takes a number and if the number is greater than 10, censors the number to 10. Have the code print the value at the end. Test your code with the following numbers:

  1. 5
  2. -9
  3. 16
  4. -25

Edit the code to censor numbers lower than -10 to -10. Retest with the numbers above to check that it works.

Can you write a for loop to test all four numbers in one block of code?

Exercise 2

Using the inflammation files from last session, find the file containing the patient with the highest average inflammation score. Print the file name, the patient number (row number) and the value of the maximum average inflammation score.

Tips:

  1. Use variables to store the maximum average and update it as you go through files and patients.
  2. Use nested loops (one loop is inside the other) to loop through the files and then patients in each file (every row).

Complete the code below:

 filenames <- list.files(path = "data", pattern = "inflammation.*csv", full.names = TRUE)
 filename_max <- "" # filename where the maximum average inflammation patient is found
 patient_max <- 0 # index (row number) for this patient in this file
 average_inf_max <- 0 # value of the average inflammation score for this patient
 for (f in filenames) {
   dat <- read.csv(file = f, header = FALSE)
   dat.means = apply(dat, 1, mean)
   for (patient_index in length(dat.means)){
     patient_average_inf = dat.means[patient_index]
     # Add your code here ...
   }
 }
 print(filename_max)
 print(patient_max)
 print(average_inf_max)

Exercise 3

Create histograms of the 7 cognitive tests using a for loop. Change th label of the x axis so that it reads “Score”. Can you make title of the plot the relevant column name (hint use colnames(dat) to extract the names of all columns.

By default R is set up to plot one image per page you can change this into a grid of multiple plots by using the following command where you specify a vector of length two, where the first number is the number of rows and the second number the number of columns. Eg for a grid of 4 plots organised as 2 rows and 2 columns.

par(mfrow = c(2,2))

Recreate plots so they fit on a single page.

The margins around the plot can also be adjusted in a similar manner. This time we give a vector of length 4 where each number specifies the number of lines to leave outside the plot on the bottom, left, top and right respectively. E.g.

par(mar = c(5,5,5,2))

Use this command to create equal margins of 4 lines.

Exercise 4

Create density plots of age separately for males and females. Can you change the colours so that the females are plotted in purple and males are plotted in green? Can you add the argument xlim so that each plot has the same x-axis?

Exercise 5

Write a for loop to test whether any of the 7 cognitive tests are significantly different between the cases and controls (use a t-test).

Record all p-values and mean differences but only produce boxplots if the p-value is < 0.05.

Tips:

To store the output you need to create an empty vector. This can be done using the rep() function which repeats a given object (either a single value or vector) a specified number of times. E.g

rep(0,4)
## [1] 0 0 0 0

Use rep() to create a vector of NA before you start your for loop and replace the values for each test in turn.

Use mtext() to add the p value to plot. Use signif() to round the p value to 2 significant figures.

Solutions