Download the data and read it into R. Before starting the exercise, convert the subject ID (Subject) to a factor:

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)
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

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

library(dplyr)
theoph <- read.csv("theoph.csv")
theoph$Subject = factor(theoph$Subject)
theoph_timeandconc <- select(theoph, Subject, Time, conc)
head(theoph_timeandconc)
##   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

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

library(dplyr)
theoph <- read.csv("theoph.csv")
theoph$Subject = factor(theoph$Subject)
theoph2 <- select(theoph, Subject, Dose, Wt)
theoph2 %>%
  distinct(Subject, Dose, Wt) %>%
  head()
##   Subject Dose   Wt
## 1       1 4.02 79.6
## 2       2 4.40 72.4
## 3       3 4.53 70.5
## 4       4 4.40 72.7
## 5       5 5.86 54.6
## 6       6 4.00 80.0

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

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

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

library(dplyr)
theoph <- read.csv("theoph.csv")
theoph$Subject = factor(theoph$Subject)
filter(theoph, Subject == 1:4)
##    Subject   Wt Dose  Time conc
## 1        1 79.6 4.02  0.00 0.74
## 2        1 79.6 4.02  2.02 9.66
## 3        1 79.6 4.02  9.05 6.89
## 4        2 72.4 4.40  0.52 7.91
## 5        2 72.4 4.40  5.02 6.08
## 6        2 72.4 4.40 24.30 0.90
## 7        3 70.5 4.53  0.00 0.00
## 8        3 70.5 4.53  2.02 7.80
## 9        3 70.5 4.53  9.00 4.90
## 10       4 72.7 4.40  0.60 4.60
## 11       4 72.7 4.40  5.02 6.88
## 12       4 72.7 4.40 24.65 1.15

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

theoph %>%
  group_by(Subject) %>%
  summarise(conc = mean(conc))
## # A tibble: 12 × 2
##    Subject  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
##  7 7        3.91
##  8 8        4.27
##  9 9        4.89
## 10 10       5.93
## 11 11       4.51
## 12 12       5.41

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)
library(dplyr)
theoph <- read.csv("theoph.csv")
theoph$Subject = factor(theoph$Subject)
theoph %>%
  filter(Subject == 1) %>%
  ggplot(aes(x=Time, y=conc)) + geom_line() + ggtitle("Subject 1 treatment")

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

library(dplyr)
theoph <- read.csv("theoph.csv")
theoph$Subject = factor(theoph$Subject)
theoph %>%
  select(Subject, Time, conc) %>%
  ggplot(aes(x=Time, y=conc, col=Subject)) + geom_line() + ggtitle("Subject treatment over time")

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

library(dplyr)
theoph <- read.csv("theoph.csv")
theoph$Subject = factor(theoph$Subject)
theoph %>%
  select(Subject, Time, conc) %>%
  ggplot(aes(x=Time, y=conc)) + geom_line() + ggtitle("Subject treatment over time") + facet_wrap(~ Subject)