exercise08

Author

Hangu Lee

0. Setting

# Load the package
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
library(ggplot2)

# Load the dataset
theoph <- read.csv("./data/theoph.csv")

# Convert the subject ID (Subject) to a factor
theoph$Subject = factor(theoph$Subject)

1. Results

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

theoph_sub <- theoph |> 
  select(Subject, Time, conc)

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

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

theoph_first <- theoph |> 
  filter(Subject == 1)

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

theoph_four <- theoph |> 
  filter(Subject %in% c(1, 2, 3, 4))

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

theoph_avg_conc <- theoph |> 
  group_by(Subject) |> 
  summarise(mean_conc = mean(conc))

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

theoph |> 
  filter(Subject == 1) |> 
  ggplot(aes(x = Time, y = conc)) + 
  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 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)