### read in data ###
WC.IL <- read.csv("C:/Users/Lisa/Desktop/677/Pertussis/WC.IL.csv", stringsAsFactors = FALSE)
### this is what the data looks like ###
head(WC.IL)
## YEAR WEEK CASE
## 1 1937 1 -
## 2 1937 2 -
## 3 1937 3 -
## 4 1937 4 -
## 5 1937 5 -
## 6 1937 6 -
### change all unavailalble CASE data (-) to NA ###
for (i in 1:length(WC.IL$CASE)) {
if (WC.IL$CASE[i] == "-") {
WC.IL$CASE[i] <- NA
}
}
WC.IL$CASE <- as.numeric(WC.IL$CASE)
WC.IL$YEAR <- as.numeric(WC.IL$YEAR)
WC.IL$WEEK <- formatC(WC.IL$WEEK, width = 2, flag = "0")
### this is what the data looks like now ###
head(WC.IL)
## YEAR WEEK CASE
## 1 1937 01 NA
## 2 1937 02 NA
## 3 1937 03 NA
## 4 1937 04 NA
## 5 1937 05 NA
## 6 1937 06 NA
### create an ID for YEAR & WEEK together - YEAR.WEEK ###
YR.WK <- as.character(paste(WC.IL[, 1], WC.IL[, 2], sep = "."))
WC.IL <- data.frame(WC.IL, YR.WK)
### find yearly CASE totals ###
WC.ILs <- as.array(split(WC.IL, WC.IL$YEAR))
CASE.YR <- rep(NA, length(unique(WC.IL$YEAR)))
for (i in 1:length(unique(WC.IL$YEAR))) {
CASE.YR[i] <- sum(WC.ILs[[i]][, 3], na.rm = TRUE)
}
CASE.YR <- data.frame(YEAR = c(min(WC.IL$YEAR):max(WC.IL$YEAR)), CASE.YR)
### Merge data into one dataframe containing weekly and yearly CASE
### numbers ### CASE.YR is total of CASEs for that year ###
WC.IL <- merge(WC.IL, CASE.YR)
head(WC.IL, 106)
## YEAR WEEK CASE YR.WK CASE.YR
## 1 1937 01 NA 1937.01 708
## 2 1937 02 NA 1937.02 708
## 3 1937 03 NA 1937.03 708
## 4 1937 04 NA 1937.04 708
## 5 1937 05 NA 1937.05 708
## 6 1937 06 NA 1937.06 708
## 7 1937 07 NA 1937.07 708
## 8 1937 08 NA 1937.08 708
## 9 1937 09 NA 1937.09 708
## 10 1937 10 NA 1937.10 708
## 11 1937 11 NA 1937.11 708
## 12 1937 12 NA 1937.12 708
## 13 1937 13 NA 1937.13 708
## 14 1937 14 NA 1937.14 708
## 15 1937 15 NA 1937.15 708
## 16 1937 16 NA 1937.16 708
## 17 1937 17 NA 1937.17 708
## 18 1937 18 NA 1937.18 708
## 19 1937 19 NA 1937.19 708
## 20 1937 20 NA 1937.20 708
## 21 1937 21 NA 1937.21 708
## 22 1937 22 NA 1937.22 708
## 23 1937 23 NA 1937.23 708
## 24 1937 24 NA 1937.24 708
## 25 1937 25 NA 1937.25 708
## 26 1937 26 NA 1937.26 708
## 27 1937 27 NA 1937.27 708
## 28 1937 28 NA 1937.28 708
## 29 1937 29 NA 1937.29 708
## 30 1937 30 NA 1937.30 708
## 31 1937 31 NA 1937.31 708
## 32 1937 32 NA 1937.32 708
## 33 1937 33 NA 1937.33 708
## 34 1937 34 NA 1937.34 708
## 35 1937 35 NA 1937.35 708
## 36 1937 36 NA 1937.36 708
## 37 1937 37 NA 1937.37 708
## 38 1937 38 NA 1937.38 708
## 39 1937 39 NA 1937.39 708
## 40 1937 40 NA 1937.40 708
## 41 1937 41 NA 1937.41 708
## 42 1937 42 NA 1937.42 708
## 43 1937 43 83 1937.43 708
## 44 1937 44 88 1937.44 708
## 45 1937 45 92 1937.45 708
## 46 1937 46 93 1937.46 708
## 47 1937 47 71 1937.47 708
## 48 1937 48 75 1937.48 708
## 49 1937 49 87 1937.49 708
## 50 1937 50 73 1937.50 708
## 51 1937 51 46 1937.51 708
## 52 1937 52 NA 1937.52 708
## 53 1938 01 104 1938.01 13881
## 54 1938 02 100 1938.02 13881
## 55 1938 03 124 1938.03 13881
## 56 1938 04 112 1938.04 13881
## 57 1938 05 93 1938.05 13881
## 58 1938 06 105 1938.06 13881
## 59 1938 07 76 1938.07 13881
## 60 1938 08 85 1938.08 13881
## 61 1938 09 121 1938.09 13881
## 62 1938 10 122 1938.10 13881
## 63 1938 11 94 1938.11 13881
## 64 1938 12 114 1938.12 13881
## 65 1938 13 83 1938.13 13881
## 66 1938 14 91 1938.14 13881
## 67 1938 15 96 1938.15 13881
## 68 1938 16 136 1938.16 13881
## 69 1938 17 126 1938.17 13881
## 70 1938 18 120 1938.18 13881
## 71 1938 19 132 1938.19 13881
## 72 1938 20 141 1938.20 13881
## 73 1938 21 169 1938.21 13881
## 74 1938 22 230 1938.22 13881
## 75 1938 23 232 1938.23 13881
## 76 1938 24 232 1938.24 13881
## 77 1938 25 148 1938.25 13881
## 78 1938 26 245 1938.26 13881
## 79 1938 27 292 1938.27 13881
## 80 1938 28 415 1938.28 13881
## 81 1938 29 499 1938.29 13881
## 82 1938 30 463 1938.30 13881
## 83 1938 31 491 1938.31 13881
## 84 1938 32 483 1938.32 13881
## 85 1938 33 NA 1938.33 13881
## 86 1938 34 346 1938.34 13881
## 87 1938 35 504 1938.35 13881
## 88 1938 36 454 1938.36 13881
## 89 1938 37 NA 1938.37 13881
## 90 1938 38 338 1938.38 13881
## 91 1938 39 334 1938.39 13881
## 92 1938 40 344 1938.40 13881
## 93 1938 41 380 1938.41 13881
## 94 1938 42 508 1938.42 13881
## 95 1938 43 490 1938.43 13881
## 96 1938 44 486 1938.44 13881
## 97 1938 45 553 1938.45 13881
## 98 1938 46 501 1938.46 13881
## 99 1938 47 447 1938.47 13881
## 100 1938 48 377 1938.48 13881
## 101 1938 49 595 1938.49 13881
## 102 1938 50 494 1938.50 13881
## 103 1938 51 341 1938.51 13881
## 104 1938 52 315 1938.52 13881
## 105 1939 01 419 1939.01 12647
## 106 1939 02 467 1939.02 12647
### create plots of weekly and yearly CASE numbers in 'lattice' ###
require(lattice)
## Loading required package: lattice
plot.wk <- barchart(CASE ~ YR.WK, data = WC.IL, horizontal = FALSE, xlab = "WEEK")
plot.yr <- barchart(CASE.YR ~ YEAR, data = WC.IL, horizontal = FALSE, col = "black",
xlab = "YEAR")
print(plot.wk, position = c(0.01, 0.47, 0.65, 0.99), more = TRUE)
print(plot.yr, position = c(0.01, 0, 0.65, 0.5), more = FALSE)
### same plots plotted using 'plot' ###
par(mfrow = c(2, 1), mar = c(4, 4, 1, 1))
plot(WC.IL$YR.WK, WC.IL$CASE, type = "p", pch = ".", xlab = "WEEK")
plot(WC.IL$YEAR, WC.IL$CASE.YR, type = "b", pch = 16, xlab = "YEAR")
### split the data by year and plot CASE numbers by week for each year ###
### note that CASEs for each month are NOT plotted on the same scale ###
WC.ILc <- WC.IL[complete.cases(WC.IL), ]
WC.ILcs <- as.array(split(WC.ILc, WC.ILc$YEAR))
par(mfrow = c(1, 1))
for (i in 1:length(unique(WC.ILc$YEAR))) {
plot(WC.ILcs[[i]][, 2], WC.ILcs[[i]][, 3], type = "b", xlab = WC.ILcs[[i]][1,
1], ylab = "Cases", pch = 16)
}