Module08_Exercise: Theophylline Data Manipulation

Author

Cienna Kim

Published

June 17, 2026

Introduction: Data Loading and Setup

library(dplyr)
library(ggplot2)

theoph <- read.csv("./data/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

1. Select Subject, Time and conc

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

2. One Row for Each Subject with Subject, 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

3. Only the First Subject

theoph_first_subject <- theoph |>
  filter(Subject == "1")

theoph_first_subject
   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
7        1 79.6 4.02  5.10  8.36
8        1 79.6 4.02  7.03  7.47
9        1 79.6 4.02  9.05  6.89
10       1 79.6 4.02 12.12  5.94
11       1 79.6 4.02 24.37  3.28

4. Only for First Four Subject

theoph_first_four <- theoph |>
  filter(Subject %in% c("1", "2", "3", "4"))

head(theoph_first_four)
  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

5. Average Concentration Per Subject

theoph_avg_conc <- theoph |>
  group_by(Subject) |>
  summarise(avgConc = mean(conc))

theoph_avg_conc
# A tibble: 12 × 2
   Subject avgConc
   <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

6. First Subject only: Lineplot

theoph |>
  filter(Subject == "1") |>
  ggplot(aes(x = Time, y = conc)) +
  geom_line() +
  labs(x = "Time", y = "conc")

7. Subject, Time, and conc: Lineplot Colored by Subject

theoph |>
  select(Subject, Time, conc) |>
  ggplot(aes(x = Time, y = conc, color = Subject)) +
  geom_line() +
  labs(x = "Time", y = "conc")

8. Subject, Time, and conc: Faceted Lineplot by Subject

theoph |>
  select(Subject, Time, conc) |>
  ggplot(aes(x = Time, y = conc)) +
  geom_line() +
  facet_wrap(~Subject) +
  labs(x = "Time", y = "conc")