Load the desired file(s) and package(s).
theoph <- read.csv("theoph.csv")
theoph$Subject = factor(theoph$Subject)
library(knitr)
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
Using select() make a new data frame containing only the subject ID,
the Time and conc variables.
theoph_stc <- select(theoph, Subject, Time, conc)
head(theoph_stc)
## 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.
theoph_swd <- distinct(select(theoph, Subject, Wt, Dose), .keep_all = TRUE)
head(theoph_swd)
## 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
Using filter() make a new data frame containing only the first
subject.
subject_1 <- filter(theoph, Subject == 1)
head(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
Using filter() make a new data frame containing only the first four
subjects.
subject_4 <- filter(theoph, Subject %in% unique(theoph$Subject)[1:4])
head(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
Using group_by(), calculate the average concentration per subject
(across all times).
theoph %>%
group_by(Subject) %>%
summarise(avg_conc = mean(conc))
## # A tibble: 12 × 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
## 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)
theoph %>%
filter(Subject == 1) %>%
ggplot(aes(x=Time, y=conc)) + geom_line() + ggtitle("Subject 1 Theoph")

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

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)) + geom_line() + facet_wrap(~Subject)
