Measles in IL data

### read in data ###
M.IL <- read.csv("C:/Users/Lisa/Desktop/677/Measles/M.IL.csv", stringsAsFactors = FALSE)

### this is what the data looks like ###
head(M.IL)
##   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.IL$CASE)) {
    if (M.IL$CASE[i] == "-") {
        M.IL$CASE[i] <- NA
    }
}

M.IL$CASE <- as.numeric(M.IL$CASE)
M.IL$YEAR <- as.numeric(M.IL$YEAR)
M.IL$WEEK <- formatC(M.IL$WEEK, width = 2, flag = "0")

### this is what the data looks like now ###
head(M.IL)
##   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.IL[, 1], M.IL[, 2], sep = "."))
M.IL <- data.frame(M.IL, YR.WK)

### find yearly CASE totals ###

M.ILs <- as.array(split(M.IL, M.IL$YEAR))

CASE.YR <- rep(NA, length(unique(M.IL$YEAR)))

for (i in 1:length(unique(M.IL$YEAR))) {
    CASE.YR[i] <- sum(M.ILs[[i]][, 3], na.rm = TRUE)
}

CASE.YR <- data.frame(YEAR = c(min(M.IL$YEAR):max(M.IL$YEAR)), CASE.YR)

### Merge data into one dataframe containing weekly and yearly CASE
### numbers ### CASE.YR is total of CASEs for that year ###
M.IL <- merge(M.IL, CASE.YR)
head(M.IL, 106)
##     YEAR WEEK CASE   YR.WK CASE.YR
## 1   1927   01   NA 1927.01     138
## 2   1927   02   NA 1927.02     138
## 3   1927   03   NA 1927.03     138
## 4   1927   04   NA 1927.04     138
## 5   1927   05   NA 1927.05     138
## 6   1927   06   NA 1927.06     138
## 7   1927   07   NA 1927.07     138
## 8   1927   08   NA 1927.08     138
## 9   1927   09   NA 1927.09     138
## 10  1927   10   NA 1927.10     138
## 11  1927   11   NA 1927.11     138
## 12  1927   12   NA 1927.12     138
## 13  1927   13   NA 1927.13     138
## 14  1927   14   NA 1927.14     138
## 15  1927   15   NA 1927.15     138
## 16  1927   16   NA 1927.16     138
## 17  1927   17   NA 1927.17     138
## 18  1927   18   NA 1927.18     138
## 19  1927   19   NA 1927.19     138
## 20  1927   20   NA 1927.20     138
## 21  1927   21   NA 1927.21     138
## 22  1927   22   NA 1927.22     138
## 23  1927   23   NA 1927.23     138
## 24  1927   24   NA 1927.24     138
## 25  1927   25   NA 1927.25     138
## 26  1927   26   NA 1927.26     138
## 27  1927   27   NA 1927.27     138
## 28  1927   28   NA 1927.28     138
## 29  1927   29   NA 1927.29     138
## 30  1927   30   NA 1927.30     138
## 31  1927   31   NA 1927.31     138
## 32  1927   32   NA 1927.32     138
## 33  1927   33   NA 1927.33     138
## 34  1927   34   NA 1927.34     138
## 35  1927   35   NA 1927.35     138
## 36  1927   36   NA 1927.36     138
## 37  1927   37   NA 1927.37     138
## 38  1927   38   NA 1927.38     138
## 39  1927   39   NA 1927.39     138
## 40  1927   40   NA 1927.40     138
## 41  1927   41   NA 1927.41     138
## 42  1927   42   NA 1927.42     138
## 43  1927   43   NA 1927.43     138
## 44  1927   44   NA 1927.44     138
## 45  1927   45   NA 1927.45     138
## 46  1927   46   NA 1927.46     138
## 47  1927   47   32 1927.47     138
## 48  1927   48   15 1927.48     138
## 49  1927   49   32 1927.49     138
## 50  1927   50   NA 1927.50     138
## 51  1927   51   33 1927.51     138
## 52  1927   52   26 1927.52     138
## 53  1928   01   38 1928.01    7460
## 54  1928   02   58 1928.02    7460
## 55  1928   03   46 1928.03    7460
## 56  1928   04   61 1928.04    7460
## 57  1928   05   84 1928.05    7460
## 58  1928   06  101 1928.06    7460
## 59  1928   07  103 1928.07    7460
## 60  1928   08  156 1928.08    7460
## 61  1928   09  151 1928.09    7460
## 62  1928   10  149 1928.10    7460
## 63  1928   11  260 1928.11    7460
## 64  1928   12  232 1928.12    7460
## 65  1928   13  180 1928.13    7460
## 66  1928   14  226 1928.14    7460
## 67  1928   15  164 1928.15    7460
## 68  1928   16  234 1928.16    7460
## 69  1928   17  173 1928.17    7460
## 70  1928   18  244 1928.18    7460
## 71  1928   19  275 1928.19    7460
## 72  1928   20  214 1928.20    7460
## 73  1928   21  244 1928.21    7460
## 74  1928   22  268 1928.22    7460
## 75  1928   23  195 1928.23    7460
## 76  1928   24  216 1928.24    7460
## 77  1928   25  186 1928.25    7460
## 78  1928   26  185 1928.26    7460
## 79  1928   27  123 1928.27    7460
## 80  1928   28   90 1928.28    7460
## 81  1928   29   81 1928.29    7460
## 82  1928   30   47 1928.30    7460
## 83  1928   31   49 1928.31    7460
## 84  1928   32   29 1928.32    7460
## 85  1928   33   26 1928.33    7460
## 86  1928   34   24 1928.34    7460
## 87  1928   35   24 1928.35    7460
## 88  1928   36   14 1928.36    7460
## 89  1928   37   23 1928.37    7460
## 90  1928   38   43 1928.38    7460
## 91  1928   39   40 1928.39    7460
## 92  1928   40   38 1928.40    7460
## 93  1928   41   69 1928.41    7460
## 94  1928   42   75 1928.42    7460
## 95  1928   43   92 1928.43    7460
## 96  1928   44  141 1928.44    7460
## 97  1928   45  176 1928.45    7460
## 98  1928   46  116 1928.46    7460
## 99  1928   47  318 1928.47    7460
## 100 1928   48  213 1928.48    7460
## 101 1928   49  331 1928.49    7460
## 102 1928   50  277 1928.50    7460
## 103 1928   51  311 1928.51    7460
## 104 1928   52  247 1928.52    7460
## 105 1929   01  330 1929.01   38503
## 106 1929   02  339 1929.02   38503

### create plots of weekly and yearly CASE numbers in 'lattice' ###
require(lattice)
## Loading required package: lattice
plot.wk <- barchart(CASE ~ YR.WK, data = M.IL, horizontal = FALSE, xlab = "WEEK")
plot.yr <- barchart(CASE.YR ~ YEAR, data = M.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)

plot of chunk unnamed-chunk-1


### same plots plotted using 'plot' ###
par(mfrow = c(2, 1), mar = c(4, 4, 1, 1))
plot(M.IL$YR.WK, M.IL$CASE, type = "p", pch = ".", xlab = "WEEK")
plot(M.IL$YEAR, M.IL$CASE.YR, type = "b", pch = 16, xlab = "YEAR")

plot of chunk unnamed-chunk-1



### 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.ILc <- M.IL[complete.cases(M.IL), ]
M.ILcs <- as.array(split(M.ILc, M.ILc$YEAR))

par(mfrow = c(1, 1))
for (i in 1:length(unique(M.ILc$YEAR))) {
    plot(M.ILcs[[i]][, 2], M.ILcs[[i]][, 3], type = "b", xlab = M.ILcs[[i]][1, 
        1], ylab = "Cases", pch = 16)
}

plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1