This exercise uses the file theop.csv, which contains information 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.

theoph <- read.csv("theoph.csv")

theoph$Subject = factor(theoph$Subject)

Step one: Using select() make a new data frame containing only the subject ID, the Time and conc variables

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

Step two: Using a combination of select() and distinct() make a new data frame containing a single row for each subject with the subject ID, their weight and the dose

theoph_sub2 <- select(theoph, Subject, Wt, Dose)
head(theoph_sub2)
##   Subject   Wt Dose
## 1       1 79.6 4.02
## 2       1 79.6 4.02
## 3       1 79.6 4.02
## 4       1 79.6 4.02
## 5       1 79.6 4.02
## 6       1 79.6 4.02
theoph_sub3<- theoph_sub2 %>%
  distinct(Subject, Wt, Dose)
head(theoph_sub3)
##   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

Step three: Using filter() make a new data frame containing only the first subject

theoph_Subject1 <- filter(theoph, 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

Step four: Using filter() make a new data frame containing only the first four subjects

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

Step five: Using group_by(), calculate the average concentration per subject (across all times)

theoph_avgconc_by_subject<- theoph %>% 
  group_by(Subject) %>%
  summarise(avgconc = mean(conc))

head(theoph_avgconc_by_subject)
## # A tibble: 6 × 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

Step six: Using filter() make a new data frame containing only the first subject and pipe this to ggplot() to make a line plot with Time on the x-axis and conc on the y-axis

library(ggplot2)

theoph %>% 
  filter(Subject == 1) %>%
  ggplot(aes(x=Time, y=conc)) + geom_line() + ggtitle("Subject 1's Theophylline Concentration Over Time")

Step seven: Using select() to extract just the Subject, Time and conc variables and pipe this to ggplot() to make a line plot with Time on the x-axis, conc on the y-axis and the lines colored by subject

theoph %>% 
  select(Subject, Time, conc) %>%
  ggplot(aes(x=Time, y=conc, col=Subject)) + geom_line()

Step eight: Using select() to extract just the Subject, Time and conc variables and pipe this to ggplot to make a line plot with Time on the x-axis, conc on the y-axis and faceted by the subject ID

theoph %>% 
  select(Subject, Time, conc) %>%
  ggplot(aes(x=Time, y=conc, col=Subject)) + geom_line() + facet_wrap(~Subject)