Module 8

Author

K M

Module 8

Reading in data and downloading packages

theo<-read.csv("~/Desktop/GEOG 5680/Data/theoph.csv")
theo$Subject = factor(theo$Subject)
str(theo)
'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 ...
library(knitr)
Warning: package 'knitr' was built under R version 4.5.2
library(tidyverse)
Warning: package 'ggplot2' was built under R version 4.5.2
Warning: package 'dplyr' was built under R version 4.5.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.2
✔ ggplot2   4.0.3     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(palmerpenguins)

Attaching package: 'palmerpenguins'

The following objects are masked from 'package:datasets':

    penguins, penguins_raw
library(ggplot2)

Exercises

theo_sub_time_conc<-theo|>
  select(Subject,Time,conc)
head(theo_sub_time_conc)
  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
theo_sub_wt_dose<-theo |>
  select(Subject, Wt, Dose) |>
  distinct()
head(theo_sub_wt_dose)
  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
theo_sub1<- theo |>
  filter(Subject == 1)
head(theo_sub1)
  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
theo_sub1.4<- theo |>
  filter(Subject %in% c(1,2,3,4))
head(theo_sub1.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
theo |>
  group_by(Subject) |>
  summarise(avgConc = mean(conc))
# A tibble: 12 × 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
 7 7          3.91
 8 8          4.27
 9 9          4.89
10 10         5.93
11 11         4.51
12 12         5.41
theo_sub1 |>
  ggplot(aes(x = Time, y = conc)) +
  geom_line()+
  xlab("Hours")+ ylab("Theophylline Concentration") +
  ggtitle("Subject 1 Theophylline Concentration over time")

theo |>
  select(Subject, Time, conc) |>
  ggplot(aes(x = Time, y = conc)) +
  geom_line(aes(colour = Subject))+
  xlab("Hours")+ ylab("Theophylline Concentration") +
  ggtitle("Theophylline Concentration over time")

theo |>
  select(Subject, Time, conc) |>
  ggplot(aes(x = Time, y = conc)) +
  geom_line(aes(colour = Subject))+
  facet_wrap(~Subject)+
  xlab("Hours")+ ylab("Theophylline Concentration") +
  ggtitle("Theophylline Concentration over time")