Exercise 1 – Transforming and analysing mental health data

The aim of this exercise is to use the basic R functionality covered in the training content to transform and analyse a mental health data set. In the exercise you will import data, subset the data, conduct tabulation and create a for loop to analyse patient length of stay.

  1. Set the working directory
  2. Import the data set PD_Data.csv
  3. Calculate length of stay for all patients (you will need to convert the dates using as.date)
  4. Subset the data by the four care settings (setting column)
  5. Table ward/team and ICD-10 code for each subset
  6. Table ICD-10 code by cluster for each subset
  7. Create a for loop to calculate the total and mean length of stay for each unique patient
    1. Create data frame with just client ID and length of stay
    2. Remove NA values
    3. Create a vector of unique client ID’s
    4. Create vectors of zero for the total and mean values
    5. Begin the for loop
    6. Subset for each client ID
    7. Calculate the total length of stay for each client ID
    8. Calculate the mean length of stay for each client ID
    9. End the for loop
    10. Bind the unique client ID, total length of stay and mean length of stay vectors

Example code for the completion of this exercise can be found here. Don’t worry if you have achieved the same outcome a slightly different way, there are many ways to do the same task.