===== The exercise for this lab uses data from the file theoph.csv. This contains results from a study on the pharmacokinetics of theophylline, an anti-asthmatic drug. Twelve subjects were given oral doses of theophylline then serum concentrations were measured at 11 time points over the next 25 hours. The variables include the subject ID, their weight, the dose of the drug administered, the time at which samples were drawn and the concentration of the drug. =====

- Load the dataset:
theoph <- read.csv("theoph.csv")
- Convert Subject to a factor:
theoph$Subject <- factor(theoph$Subject)
- Load library:
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.2.3
## 
## 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
- Creating & select new data frame with Subject ID, Time, & concs:
new_df <- select(theoph, Subject, Time, conc)
- Creating a new distint data frame & select the unique Subject ID, Weight, & Dose:
distinct_df <- select(distinct(theoph, Subject, Wt, Dose), Subject, Wt, Dose)
- Creating a new data frame & filter only for the first Subject:
first_subject <- filter(theoph, Subject == levels(Subject)[1])
- Creating & filter with the new data frame of the first four Subjects:
first_four_subjects <- filter(theoph, Subject %in% levels(Subject)[1:4])
- Calculating the average concentration per Subject using group_by():
avgerage_concentration <- theoph %>%
  group_by(Subject) %>%
  summarize(avg_concentration = mean(conc))
- Load library for plotting:
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.3
- Create a new first subject data frame using filter():
old_guy <- filter(first_subject, Subject == levels(Subject)[1])
- Use ggplot() to create a line plot for the old_guy with Time on the x-axis & conc on the y-axis:
old_guy %>%
  filter(Subject=="1") %>%
  ggplot(aes(x = Time, y = conc)) + geom_line() + ggtitle("Subject 1")

- Use the select() to create a line plot with Time on the x-axis & conc on y-axis with lines colored:
theoph %>%
  select(Subject, Time, conc) %>%
  ggplot(aes(x = Time, y = conc, color = Subject)) + geom_line(aes(column = Subject)) 
## Warning in geom_line(aes(column = Subject)): Ignoring unknown aesthetics:
## column

- Creating a line plot with Time on the x-axis, conc on the y-axis, & faceted by Subject ID:
theoph %>%
  select(Subject, Time, conc) %>%
  ggplot(aes(x = Time, y = conc)) + geom_line() + facet_wrap(~ Subject)

** End of Script ****