### read in data ###
TB.FL <- read.csv("C:/Users/Lisa/Desktop/677/TB/TB.FL.csv", stringsAsFactors = FALSE)
### this is what the data looks like ###
head(TB.FL)
## YEAR WEEK CASE
## 1 1975 1 22
## 2 1975 2 33
## 3 1975 3 32
## 4 1975 4 32
## 5 1975 5 27
## 6 1975 6 34
### change all unavailalble CASE data (-) to NA ###
for (i in 1:length(TB.FL$CASE)) {
if (TB.FL$CASE[i] == "-") {
TB.FL$CASE[i] <- NA
}
}
TB.FL$CASE <- as.numeric(TB.FL$CASE)
TB.FL$YEAR <- as.numeric(TB.FL$YEAR)
TB.FL$WEEK <- formatC(TB.FL$WEEK, width = 2, flag = "0")
### this is what the data looks like now ###
head(TB.FL)
## YEAR WEEK CASE
## 1 1975 01 22
## 2 1975 02 33
## 3 1975 03 32
## 4 1975 04 32
## 5 1975 05 27
## 6 1975 06 34
### create an ID for YEAR & WEEK together - YEAR.WEEK ###
YR.WK <- as.character(paste(TB.FL[, 1], TB.FL[, 2], sep = "."))
TB.FL <- data.frame(TB.FL, YR.WK)
### find yearly CASE totals ###
TB.FLs <- as.array(split(TB.FL, TB.FL$YEAR))
CASE.YR <- rep(NA, length(unique(TB.FL$YEAR)))
for (i in 1:length(unique(TB.FL$YEAR))) {
CASE.YR[i] <- sum(TB.FLs[[i]][, 3], na.rm = TRUE)
}
CASE.YR <- data.frame(YEAR = c(min(TB.FL$YEAR):max(TB.FL$YEAR)), CASE.YR)
### Merge data into one dataframe containing weekly and yearly CASE
### numbers ### CASE.YR is total of CASEs for that year ###
TB.FL <- merge(TB.FL, CASE.YR)
head(TB.FL, 106)
## YEAR WEEK CASE YR.WK CASE.YR
## 1 1975 01 22 1975.01 1680
## 2 1975 02 33 1975.02 1680
## 3 1975 03 32 1975.03 1680
## 4 1975 04 32 1975.04 1680
## 5 1975 05 27 1975.05 1680
## 6 1975 06 34 1975.06 1680
## 7 1975 07 41 1975.07 1680
## 8 1975 08 25 1975.08 1680
## 9 1975 09 22 1975.09 1680
## 10 1975 10 47 1975.10 1680
## 11 1975 11 62 1975.11 1680
## 12 1975 12 26 1975.12 1680
## 13 1975 13 42 1975.13 1680
## 14 1975 14 42 1975.14 1680
## 15 1975 15 27 1975.15 1680
## 16 1975 16 33 1975.16 1680
## 17 1975 17 32 1975.17 1680
## 18 1975 18 42 1975.18 1680
## 19 1975 19 39 1975.19 1680
## 20 1975 20 42 1975.20 1680
## 21 1975 21 25 1975.21 1680
## 22 1975 22 37 1975.22 1680
## 23 1975 23 33 1975.23 1680
## 24 1975 24 44 1975.24 1680
## 25 1975 25 27 1975.25 1680
## 26 1975 26 53 1975.26 1680
## 27 1975 27 21 1975.27 1680
## 28 1975 28 19 1975.28 1680
## 29 1975 29 41 1975.29 1680
## 30 1975 30 42 1975.30 1680
## 31 1975 31 37 1975.31 1680
## 32 1975 32 39 1975.32 1680
## 33 1975 33 27 1975.33 1680
## 34 1975 34 37 1975.34 1680
## 35 1975 35 31 1975.35 1680
## 36 1975 36 15 1975.36 1680
## 37 1975 37 37 1975.37 1680
## 38 1975 38 30 1975.38 1680
## 39 1975 39 37 1975.39 1680
## 40 1975 40 38 1975.40 1680
## 41 1975 41 36 1975.41 1680
## 42 1975 42 21 1975.42 1680
## 43 1975 43 62 1975.43 1680
## 44 1975 44 26 1975.44 1680
## 45 1975 45 NA 1975.45 1680
## 46 1975 46 17 1975.46 1680
## 47 1975 47 30 1975.47 1680
## 48 1975 48 19 1975.48 1680
## 49 1975 49 31 1975.49 1680
## 50 1975 50 30 1975.50 1680
## 51 1975 51 NA 1975.51 1680
## 52 1975 52 36 1975.52 1680
## 53 1975 53 NA 1975.53 1680
## 54 1976 01 19 1976.01 1632
## 55 1976 02 23 1976.02 1632
## 56 1976 03 17 1976.03 1632
## 57 1976 04 23 1976.04 1632
## 58 1976 05 42 1976.05 1632
## 59 1976 06 32 1976.06 1632
## 60 1976 07 36 1976.07 1632
## 61 1976 08 25 1976.08 1632
## 62 1976 09 34 1976.09 1632
## 63 1976 10 19 1976.10 1632
## 64 1976 11 18 1976.11 1632
## 65 1976 12 26 1976.12 1632
## 66 1976 13 50 1976.13 1632
## 67 1976 14 30 1976.14 1632
## 68 1976 15 31 1976.15 1632
## 69 1976 16 47 1976.16 1632
## 70 1976 17 41 1976.17 1632
## 71 1976 18 30 1976.18 1632
## 72 1976 19 30 1976.19 1632
## 73 1976 20 26 1976.20 1632
## 74 1976 21 28 1976.21 1632
## 75 1976 22 24 1976.22 1632
## 76 1976 23 41 1976.23 1632
## 77 1976 24 23 1976.24 1632
## 78 1976 25 46 1976.25 1632
## 79 1976 26 37 1976.26 1632
## 80 1976 27 27 1976.27 1632
## 81 1976 28 37 1976.28 1632
## 82 1976 29 47 1976.29 1632
## 83 1976 30 41 1976.30 1632
## 84 1976 31 32 1976.31 1632
## 85 1976 32 28 1976.32 1632
## 86 1976 33 25 1976.33 1632
## 87 1976 34 41 1976.34 1632
## 88 1976 35 19 1976.35 1632
## 89 1976 36 30 1976.36 1632
## 90 1976 37 38 1976.37 1632
## 91 1976 38 29 1976.38 1632
## 92 1976 39 43 1976.39 1632
## 93 1976 40 39 1976.40 1632
## 94 1976 41 22 1976.41 1632
## 95 1976 42 39 1976.42 1632
## 96 1976 43 24 1976.43 1632
## 97 1976 44 18 1976.44 1632
## 98 1976 45 35 1976.45 1632
## 99 1976 46 33 1976.46 1632
## 100 1976 47 12 1976.47 1632
## 101 1976 48 51 1976.48 1632
## 102 1976 49 42 1976.49 1632
## 103 1976 50 34 1976.50 1632
## 104 1976 51 36 1976.51 1632
## 105 1976 52 12 1976.52 1632
## 106 1977 01 NA 1977.01 1654
### create plots of weekly and yearly CASE numbers in 'lattice' ###
require(lattice)
## Loading required package: lattice
plot.wk <- barchart(CASE ~ YR.WK, data = TB.FL, horizontal = FALSE, xlab = "WEEK")
plot.yr <- barchart(CASE.YR ~ YEAR, data = TB.FL, 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(TB.FL$YR.WK, TB.FL$CASE, type = "p", pch = ".", xlab = "WEEK")
plot(TB.FL$YEAR, TB.FL$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 ###
TB.FLc <- TB.FL[complete.cases(TB.FL), ]
TB.FLcs <- as.array(split(TB.FLc, TB.FLc$YEAR))
par(mfrow = c(1, 1))
for (i in 1:length(unique(TB.FLc$YEAR))) {
plot(TB.FLcs[[i]][, 2], TB.FLcs[[i]][, 3], type = "b", xlab = TB.FLcs[[i]][1,
1], ylab = "Cases", pch = 16)
}