Module 08 dplyr Exercises: Theophylline Data

Author

Zach Grube

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

1 + 1
[1] 2

You can add options to executable code like this

[1] 4

The echo: false option disables the printing of code (only output is displayed).

Setup

This report uses the theoph.csv dataset, which contains the results of a pharmacokinetic study of theophylline. The subject ID is converted to a factor before completing the exercises.

library(dplyr)
library(ggplot2)
theoph <- read.csv("C:/Users/yolom/OneDrive/Desktop/GEOG5680/Module 8/theoph.csv")
theoph$Subject <- factor(theoph$Subject)
str(theoph)
'data.frame':   132 obs. of  5 variables:
 $ Subject: Factor w/ 12 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Wt     : num  79.6 79.6 79.6 79.6 79.6 79.6 79.6 79.6 79.6 79.6 ...
 $ Dose   : num  4.02 4.02 4.02 4.02 4.02 4.02 4.02 4.02 4.02 4.02 ...
 $ Time   : num  0 0.25 0.57 1.12 2.02 ...
 $ conc   : num  0.74 2.84 6.57 10.5 9.66 8.58 8.36 7.47 6.89 5.94 ...
head(theoph)
  Subject   Wt Dose Time  conc
1       1 79.6 4.02 0.00  0.74
2       1 79.6 4.02 0.25  2.84
3       1 79.6 4.02 0.57  6.57
4       1 79.6 4.02 1.12 10.50
5       1 79.6 4.02 2.02  9.66
6       1 79.6 4.02 3.82  8.58

Exercise 1

Make a new data frame containing only the subject ID, Time, and conc variables.

theoph_time_conc <- theoph |>
  select(Subject, Time, conc)
head(theoph_time_conc)
  Subject Time  conc
1       1 0.00  0.74
2       1 0.25  2.84
3       1 0.57  6.57
4       1 1.12 10.50
5       1 2.02  9.66
6       1 3.82  8.58

Exercise 2

Make a new data frame containing a single row for each subject with the subject ID, weight, and dose.

theoph_subject_info <- theoph |>
  select(Subject, Wt, Dose) |>
  distinct()
theoph_subject_info
   Subject   Wt Dose
1        1 79.6 4.02
2        2 72.4 4.40
3        3 70.5 4.53
4        4 72.7 4.40
5        5 54.6 5.86
6        6 80.0 4.00
7        7 64.6 4.95
8        8 70.5 4.53
9        9 86.4 3.10
10      10 58.2 5.50
11      11 65.0 4.92
12      12 60.5 5.30

Exercise 3

Make a new data frame containing only the first subject.

theoph_subject1 <- theoph |>
  filter(Subject == "1")
head(theoph_subject1)
  Subject   Wt Dose Time  conc
1       1 79.6 4.02 0.00  0.74
2       1 79.6 4.02 0.25  2.84
3       1 79.6 4.02 0.57  6.57
4       1 79.6 4.02 1.12 10.50
5       1 79.6 4.02 2.02  9.66
6       1 79.6 4.02 3.82  8.58

Exercise 4

Make a new data frame containing only the first four subjects.

theoph_subjects1to4 <- theoph |>
  filter(Subject %in% c("1", "2", "3", "4"))
head(theoph_subjects1to4)
  Subject   Wt Dose Time  conc
1       1 79.6 4.02 0.00  0.74
2       1 79.6 4.02 0.25  2.84
3       1 79.6 4.02 0.57  6.57
4       1 79.6 4.02 1.12 10.50
5       1 79.6 4.02 2.02  9.66
6       1 79.6 4.02 3.82  8.58

Exercise 5

Calculate the average concentration per subject across all times.

avg_conc_by_subject <- theoph |>
  group_by(Subject) |>
  summarise(avg_conc = mean(conc))
avg_conc_by_subject
# A tibble: 12 × 2
   Subject avg_conc
   <fct>      <dbl>
 1 1           6.44
 2 2           4.82
 3 3           5.09
 4 4           4.94
 5 5           5.78
 6 6           3.53
 7 7           3.91
 8 8           4.27
 9 9           4.89
10 10          5.93
11 11          4.51
12 12          5.41

Exercise 6

Make a line plot for the first subject with Time on the x-axis and conc on the y-axis.

theoph |>
  filter(Subject == "1") |>
  ggplot(aes(x = Time, y = conc)) +
  geom_line() +
  geom_point() +
  labs(
    title = "Theophylline Concentration Over Time: Subject 1",
    x = "Time since dose given (hr)",
    y = "Theophylline concentration (mg/L)"
  )

Exercise 7

Extract Subject, Time, and conc, then make a line plot with lines colored by subject.

theoph |>
  select(Subject, Time, conc) |>
  ggplot(aes(x = Time, y = conc, color = Subject)) +
  geom_line() +
  geom_point() +
  labs(
    title = "Theophylline Concentration Over Time by Subject",
    x = "Time since dose given (hr)",
    y = "Theophylline concentration (mg/L)",
    color = "Subject"
  )

Exercise 8

Extract Subject, Time, and conc, then make a line plot faceted by subject ID.

theoph |>
  select(Subject, Time, conc) |>
  ggplot(aes(x = Time, y = conc)) +
  geom_line() +
  geom_point() +
  facet_wrap(~ Subject) +
  labs(
    title = "Theophylline Concentration Over Time by Subject",
    x = "Time since dose given (hr)",
    y = "Theophylline concentration (mg/L)"
  )