### read in data ###
M.FL <- read.csv("C:/Users/Lisa/Desktop/677/Measles/M.FL.csv", stringsAsFactors = FALSE)
### this is what the data looks like ###
head(M.FL)
## YEAR WEEK CASE
## 1 1927 1 -
## 2 1927 2 -
## 3 1927 3 -
## 4 1927 4 -
## 5 1927 5 -
## 6 1927 6 -
### change all unavailalble CASE data (-) to NA ###
for (i in 1:length(M.FL$CASE)) {
if (M.FL$CASE[i] == "-") {
M.FL$CASE[i] <- NA
}
}
M.FL$CASE <- as.numeric(M.FL$CASE)
M.FL$YEAR <- as.numeric(M.FL$YEAR)
M.FL$WEEK <- formatC(M.FL$WEEK, width = 2, flag = "0")
### this is what the data looks like now ###
head(M.FL)
## YEAR WEEK CASE
## 1 1927 01 NA
## 2 1927 02 NA
## 3 1927 03 NA
## 4 1927 04 NA
## 5 1927 05 NA
## 6 1927 06 NA
### create an ID for YEAR & WEEK together - YEAR.WEEK ###
YR.WK <- as.character(paste(M.FL[, 1], M.FL[, 2], sep = "."))
M.FL <- data.frame(M.FL, YR.WK)
### find yearly CASE totals ###
M.FLs <- as.array(split(M.FL, M.FL$YEAR))
CASE.YR <- rep(NA, length(unique(M.FL$YEAR)))
for (i in 1:length(unique(M.FL$YEAR))) {
CASE.YR[i] <- sum(M.FLs[[i]][, 3], na.rm = TRUE)
}
CASE.YR <- data.frame(YEAR = c(min(M.FL$YEAR):max(M.FL$YEAR)), CASE.YR)
### Merge data into one dataframe containing weekly and yearly CASE
### numbers ### CASE.YR is total of CASEs for that year ###
M.FL <- merge(M.FL, CASE.YR)
head(M.FL, 106)
## YEAR WEEK CASE YR.WK CASE.YR
## 1 1927 01 NA 1927.01 19
## 2 1927 02 NA 1927.02 19
## 3 1927 03 NA 1927.03 19
## 4 1927 04 NA 1927.04 19
## 5 1927 05 NA 1927.05 19
## 6 1927 06 NA 1927.06 19
## 7 1927 07 NA 1927.07 19
## 8 1927 08 NA 1927.08 19
## 9 1927 09 NA 1927.09 19
## 10 1927 10 NA 1927.10 19
## 11 1927 11 NA 1927.11 19
## 12 1927 12 NA 1927.12 19
## 13 1927 13 NA 1927.13 19
## 14 1927 14 NA 1927.14 19
## 15 1927 15 NA 1927.15 19
## 16 1927 16 NA 1927.16 19
## 17 1927 17 NA 1927.17 19
## 18 1927 18 NA 1927.18 19
## 19 1927 19 NA 1927.19 19
## 20 1927 20 NA 1927.20 19
## 21 1927 21 NA 1927.21 19
## 22 1927 22 NA 1927.22 19
## 23 1927 23 NA 1927.23 19
## 24 1927 24 NA 1927.24 19
## 25 1927 25 NA 1927.25 19
## 26 1927 26 NA 1927.26 19
## 27 1927 27 NA 1927.27 19
## 28 1927 28 NA 1927.28 19
## 29 1927 29 NA 1927.29 19
## 30 1927 30 NA 1927.30 19
## 31 1927 31 NA 1927.31 19
## 32 1927 32 NA 1927.32 19
## 33 1927 33 NA 1927.33 19
## 34 1927 34 NA 1927.34 19
## 35 1927 35 NA 1927.35 19
## 36 1927 36 NA 1927.36 19
## 37 1927 37 NA 1927.37 19
## 38 1927 38 NA 1927.38 19
## 39 1927 39 NA 1927.39 19
## 40 1927 40 NA 1927.40 19
## 41 1927 41 NA 1927.41 19
## 42 1927 42 NA 1927.42 19
## 43 1927 43 NA 1927.43 19
## 44 1927 44 NA 1927.44 19
## 45 1927 45 NA 1927.45 19
## 46 1927 46 NA 1927.46 19
## 47 1927 47 2 1927.47 19
## 48 1927 48 1 1927.48 19
## 49 1927 49 3 1927.49 19
## 50 1927 50 NA 1927.50 19
## 51 1927 51 5 1927.51 19
## 52 1927 52 8 1927.52 19
## 53 1928 01 3 1928.01 1715
## 54 1928 02 7 1928.02 1715
## 55 1928 03 6 1928.03 1715
## 56 1928 04 13 1928.04 1715
## 57 1928 05 7 1928.05 1715
## 58 1928 06 24 1928.06 1715
## 59 1928 07 19 1928.07 1715
## 60 1928 08 16 1928.08 1715
## 61 1928 09 11 1928.09 1715
## 62 1928 10 32 1928.10 1715
## 63 1928 11 48 1928.11 1715
## 64 1928 12 70 1928.12 1715
## 65 1928 13 66 1928.13 1715
## 66 1928 14 42 1928.14 1715
## 67 1928 15 81 1928.15 1715
## 68 1928 16 92 1928.16 1715
## 69 1928 17 94 1928.17 1715
## 70 1928 18 142 1928.18 1715
## 71 1928 19 101 1928.19 1715
## 72 1928 20 70 1928.20 1715
## 73 1928 21 133 1928.21 1715
## 74 1928 22 175 1928.22 1715
## 75 1928 23 79 1928.23 1715
## 76 1928 24 118 1928.24 1715
## 77 1928 25 92 1928.25 1715
## 78 1928 26 21 1928.26 1715
## 79 1928 27 47 1928.27 1715
## 80 1928 28 35 1928.28 1715
## 81 1928 29 9 1928.29 1715
## 82 1928 30 4 1928.30 1715
## 83 1928 31 13 1928.31 1715
## 84 1928 32 NA 1928.32 1715
## 85 1928 33 1 1928.33 1715
## 86 1928 34 2 1928.34 1715
## 87 1928 35 NA 1928.35 1715
## 88 1928 36 6 1928.36 1715
## 89 1928 37 NA 1928.37 1715
## 90 1928 38 NA 1928.38 1715
## 91 1928 39 NA 1928.39 1715
## 92 1928 40 3 1928.40 1715
## 93 1928 41 NA 1928.41 1715
## 94 1928 42 2 1928.42 1715
## 95 1928 43 NA 1928.43 1715
## 96 1928 44 6 1928.44 1715
## 97 1928 45 2 1928.45 1715
## 98 1928 46 NA 1928.46 1715
## 99 1928 47 4 1928.47 1715
## 100 1928 48 3 1928.48 1715
## 101 1928 49 6 1928.49 1715
## 102 1928 50 5 1928.50 1715
## 103 1928 51 5 1928.51 1715
## 104 1928 52 NA 1928.52 1715
## 105 1929 01 10 1929.01 1127
## 106 1929 02 2 1929.02 1127
### create plots of weekly and yearly CASE numbers in 'lattice' ###
require(lattice)
## Loading required package: lattice
plot.wk <- barchart(CASE ~ YR.WK, data = M.FL, horizontal = FALSE, xlab = "WEEK")
plot.yr <- barchart(CASE.YR ~ YEAR, data = M.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(M.FL$YR.WK, M.FL$CASE, type = "p", pch = ".", xlab = "WEEK")
plot(M.FL$YEAR, M.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 ###
M.FLc <- M.FL[complete.cases(M.FL), ]
M.FLcs <- as.array(split(M.FLc, M.FLc$YEAR))
par(mfrow = c(1, 1))
for (i in 1:length(unique(M.FLc$YEAR))) {
plot(M.FLcs[[i]][, 2], M.FLcs[[i]][, 3], type = "b", xlab = M.FLcs[[i]][1,
1], ylab = "Cases", pch = 16)
}