TB in IL data

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

### this is what the data looks like ###
head(TB.IL)
##   YEAR WEEK CASE
## 1 1975    1   13
## 2 1975    2   30
## 3 1975    3   26
## 4 1975    4   17
## 5 1975    5   12
## 6 1975    6   42

### change all unavailalble CASE data (-) to NA ###
for (i in 1:length(TB.IL$CASE)) {
    if (TB.IL$CASE[i] == "-") {
        TB.IL$CASE[i] <- NA
    }
}

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

### this is what the data looks like now ###
head(TB.IL)
##   YEAR WEEK CASE
## 1 1975   01   13
## 2 1975   02   30
## 3 1975   03   26
## 4 1975   04   17
## 5 1975   05   12
## 6 1975   06   42

### create an ID for YEAR & WEEK together - YEAR.WEEK ###
YR.WK <- as.character(paste(TB.IL[, 1], TB.IL[, 2], sep = "."))
TB.IL <- data.frame(TB.IL, YR.WK)

### find yearly CASE totals ###

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

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

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

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

### Merge data into one dataframe containing weekly and yearly CASE
### numbers ### CASE.YR is total of CASEs for that year ###
TB.IL <- merge(TB.IL, CASE.YR)
head(TB.IL, 106)
##     YEAR WEEK CASE   YR.WK CASE.YR
## 1   1975   01   13 1975.01    1348
## 2   1975   02   30 1975.02    1348
## 3   1975   03   26 1975.03    1348
## 4   1975   04   17 1975.04    1348
## 5   1975   05   12 1975.05    1348
## 6   1975   06   42 1975.06    1348
## 7   1975   07   14 1975.07    1348
## 8   1975   08   14 1975.08    1348
## 9   1975   09   19 1975.09    1348
## 10  1975   10   25 1975.10    1348
## 11  1975   11   26 1975.11    1348
## 12  1975   12   14 1975.12    1348
## 13  1975   13   25 1975.13    1348
## 14  1975   14   22 1975.14    1348
## 15  1975   15   32 1975.15    1348
## 16  1975   16   28 1975.16    1348
## 17  1975   17   14 1975.17    1348
## 18  1975   18   22 1975.18    1348
## 19  1975   19   13 1975.19    1348
## 20  1975   20   38 1975.20    1348
## 21  1975   21   15 1975.21    1348
## 22  1975   22   15 1975.22    1348
## 23  1975   23    7 1975.23    1348
## 24  1975   24   19 1975.24    1348
## 25  1975   25   28 1975.25    1348
## 26  1975   26   37 1975.26    1348
## 27  1975   27   49 1975.27    1348
## 28  1975   28   34 1975.28    1348
## 29  1975   29   30 1975.29    1348
## 30  1975   30   11 1975.30    1348
## 31  1975   31   22 1975.31    1348
## 32  1975   32   16 1975.32    1348
## 33  1975   33   29 1975.33    1348
## 34  1975   34   55 1975.34    1348
## 35  1975   35   27 1975.35    1348
## 36  1975   36   20 1975.36    1348
## 37  1975   37   40 1975.37    1348
## 38  1975   38   35 1975.38    1348
## 39  1975   39   26 1975.39    1348
## 40  1975   40   34 1975.40    1348
## 41  1975   41    7 1975.41    1348
## 42  1975   42   32 1975.42    1348
## 43  1975   43   43 1975.43    1348
## 44  1975   44   44 1975.44    1348
## 45  1975   45   19 1975.45    1348
## 46  1975   46   37 1975.46    1348
## 47  1975   47   39 1975.47    1348
## 48  1975   48   30 1975.48    1348
## 49  1975   49   25 1975.49    1348
## 50  1975   50   33 1975.50    1348
## 51  1975   51   22 1975.51    1348
## 52  1975   52   22 1975.52    1348
## 53  1975   53   NA 1975.53    1348
## 54  1976   01    2 1976.01    1648
## 55  1976   02    2 1976.02    1648
## 56  1976   03    7 1976.03    1648
## 57  1976   04   14 1976.04    1648
## 58  1976   05   20 1976.05    1648
## 59  1976   06   44 1976.06    1648
## 60  1976   07   24 1976.07    1648
## 61  1976   08   24 1976.08    1648
## 62  1976   09   28 1976.09    1648
## 63  1976   10   16 1976.10    1648
## 64  1976   11   22 1976.11    1648
## 65  1976   12   27 1976.12    1648
## 66  1976   13   21 1976.13    1648
## 67  1976   14   78 1976.14    1648
## 68  1976   15   31 1976.15    1648
## 69  1976   16   29 1976.16    1648
## 70  1976   17   68 1976.17    1648
## 71  1976   18   NA 1976.18    1648
## 72  1976   19   20 1976.19    1648
## 73  1976   20   56 1976.20    1648
## 74  1976   21   24 1976.21    1648
## 75  1976   22   27 1976.22    1648
## 76  1976   23   44 1976.23    1648
## 77  1976   24   42 1976.24    1648
## 78  1976   25   14 1976.25    1648
## 79  1976   26   64 1976.26    1648
## 80  1976   27   65 1976.27    1648
## 81  1976   28   10 1976.28    1648
## 82  1976   29   49 1976.29    1648
## 83  1976   30   42 1976.30    1648
## 84  1976   31   33 1976.31    1648
## 85  1976   32   40 1976.32    1648
## 86  1976   33   24 1976.33    1648
## 87  1976   34   63 1976.34    1648
## 88  1976   35   52 1976.35    1648
## 89  1976   36   26 1976.36    1648
## 90  1976   37   19 1976.37    1648
## 91  1976   38   36 1976.38    1648
## 92  1976   39   18 1976.39    1648
## 93  1976   40   34 1976.40    1648
## 94  1976   41   39 1976.41    1648
## 95  1976   42   44 1976.42    1648
## 96  1976   43   19 1976.43    1648
## 97  1976   44   29 1976.44    1648
## 98  1976   45   44 1976.45    1648
## 99  1976   46   22 1976.46    1648
## 100 1976   47   32 1976.47    1648
## 101 1976   48   40 1976.48    1648
## 102 1976   49   27 1976.49    1648
## 103 1976   50   54 1976.50    1648
## 104 1976   51   21 1976.51    1648
## 105 1976   52   18 1976.52    1648
## 106 1977   01   NA 1977.01    1733

### create plots of weekly and yearly CASE numbers in 'lattice' ###
require(lattice)
## Loading required package: lattice
plot.wk <- barchart(CASE ~ YR.WK, data = TB.IL, horizontal = FALSE, xlab = "WEEK")
plot.yr <- barchart(CASE.YR ~ YEAR, data = TB.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(TB.IL$YR.WK, TB.IL$CASE, type = "p", pch = ".", xlab = "WEEK")
plot(TB.IL$YEAR, TB.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 ###
TB.ILc <- TB.IL[complete.cases(TB.IL), ]
TB.ILcs <- as.array(split(TB.ILc, TB.ILc$YEAR))

par(mfrow = c(1, 1))
for (i in 1:length(unique(TB.ILc$YEAR))) {
    plot(TB.ILcs[[i]][, 2], TB.ILcs[[i]][, 3], type = "b", xlab = TB.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