Module08

Author

Hyunjeong Sin

Read the data

library(ggplot2)
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
theoph <- read.csv("theoph.csv")
theoph$Subject = factor(theoph$Subject)

Make a new data frame (subject ID, the Time and conc) by using select()

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

Make a new data frame (subject ID, weight, dose) by using select() & distinct()

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

Make a new data frame (only the first subject) by using filter()

theoph_sub1 <- filter(theoph, Subject == "1")
head(theoph_sub1)
  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

Make a new data frame (only the first four subjects) by using filter()

theoph_sub2 <- filter(theoph, Subject %in% c("1", "2", "3", "4"))
head(theoph_sub2)
  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

Calculate the average concentration per subject by using group_by()

avg_conc <- theoph |> group_by(Subject) |> summarise(avg_conc = mean(conc))
head(avg_conc)
# A tibble: 6 × 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

Make a new data frame (only the first subject) by using filter() and make a line plot by using ggplot()

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

Extract just the Subject, Time and conc variable by using select() and make a line plot by using ggplot()

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

Same but faceted by the subject ID

theoph|> select(Subject, Time, conc) |> ggplot(aes(x = Time, y = conc, color = Subject)) + geom_line() + facet_wrap(~Subject) + ggtitle("Faceted by the Subject ID")