Module08 (5/27/2026)

Author

Ethan Schatz

This module explores the functionallity of dplyr using the Theophylline pharmacokinetic dataset. This module: - Shows the basic funcitonaly of dplyr. - Shows how ggplot2 can be incoorperated into dplyr functions.


#Project ##Data Preproccesing

library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(ggplot2)
theoph <- read.csv("E:/Summer 2026/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

##Part 1

theophSelect <- select(theoph, Subject, Time, conc)
head(theophSelect)
  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

##Part 2:

theoph |>
  select(Subject, Wt, Dose) |>
  distinct()
   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

##Part 3:

theoph |>
  filter(Subject == 1)
   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

##Part 4:

theoph |>
  filter(as.numeric(as.character(Subject)) <= 4)
   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
12       2 72.4 4.40  0.00  0.00
13       2 72.4 4.40  0.27  1.72
14       2 72.4 4.40  0.52  7.91
15       2 72.4 4.40  1.00  8.31
16       2 72.4 4.40  1.92  8.33
17       2 72.4 4.40  3.50  6.85
18       2 72.4 4.40  5.02  6.08
19       2 72.4 4.40  7.03  5.40
20       2 72.4 4.40  9.00  4.55
21       2 72.4 4.40 12.00  3.01
22       2 72.4 4.40 24.30  0.90
23       3 70.5 4.53  0.00  0.00
24       3 70.5 4.53  0.27  4.40
25       3 70.5 4.53  0.58  6.90
26       3 70.5 4.53  1.02  8.20
27       3 70.5 4.53  2.02  7.80
28       3 70.5 4.53  3.62  7.50
29       3 70.5 4.53  5.08  6.20
30       3 70.5 4.53  7.07  5.30
31       3 70.5 4.53  9.00  4.90
32       3 70.5 4.53 12.15  3.70
33       3 70.5 4.53 24.17  1.05
34       4 72.7 4.40  0.00  0.00
35       4 72.7 4.40  0.35  1.89
36       4 72.7 4.40  0.60  4.60
37       4 72.7 4.40  1.07  8.60
38       4 72.7 4.40  2.13  8.38
39       4 72.7 4.40  3.50  7.54
40       4 72.7 4.40  5.02  6.88
41       4 72.7 4.40  7.02  5.78
42       4 72.7 4.40  9.02  5.33
43       4 72.7 4.40 11.98  4.19
44       4 72.7 4.40 24.65  1.15

##Part 5

theoph |>
  group_by(Subject)|>
  summarise(avgConc = mean(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

##Part 6:

theoph |>
  filter(Subject == 1) |>
  select(Subject, Time, conc) |>
  ggplot(aes(x = Time, y = conc, group = Subject)) +geom_line() + ggtitle("Theophylline Concentration over Time")

##Part 7:

theoph |>
  select(Subject, Time, conc) |>
  ggplot(aes(x = Time, y = conc, col = Subject)) +geom_line() + ggtitle("Theophylline Concentration over Time")

##Part 8:

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

#DISCLAIMER: ChatGPT was used during the process of writing the code for the purpose of debugging and fixing errors in the code.