Pertussis in FL data

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

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

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

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

### find yearly CASE totals ###

WC.FLs <- as.array(split(WC.FL, WC.FL$YEAR))

CASE.YR <- rep(NA, length(unique(WC.FL$YEAR)))

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

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

### Merge data into one dataframe containing weekly and yearly CASE
### numbers ### CASE.YR is total of CASEs for that year ###
WC.FL <- merge(WC.FL, CASE.YR)
head(WC.FL, 106)
##     YEAR WEEK CASE   YR.WK CASE.YR
## 1   1937   01   NA 1937.01      40
## 2   1937   02   NA 1937.02      40
## 3   1937   03   NA 1937.03      40
## 4   1937   04   NA 1937.04      40
## 5   1937   05   NA 1937.05      40
## 6   1937   06   NA 1937.06      40
## 7   1937   07   NA 1937.07      40
## 8   1937   08   NA 1937.08      40
## 9   1937   09   NA 1937.09      40
## 10  1937   10   NA 1937.10      40
## 11  1937   11   NA 1937.11      40
## 12  1937   12   NA 1937.12      40
## 13  1937   13   NA 1937.13      40
## 14  1937   14   NA 1937.14      40
## 15  1937   15   NA 1937.15      40
## 16  1937   16   NA 1937.16      40
## 17  1937   17   NA 1937.17      40
## 18  1937   18   NA 1937.18      40
## 19  1937   19   NA 1937.19      40
## 20  1937   20   NA 1937.20      40
## 21  1937   21   NA 1937.21      40
## 22  1937   22   NA 1937.22      40
## 23  1937   23   NA 1937.23      40
## 24  1937   24   NA 1937.24      40
## 25  1937   25   NA 1937.25      40
## 26  1937   26   NA 1937.26      40
## 27  1937   27   NA 1937.27      40
## 28  1937   28   NA 1937.28      40
## 29  1937   29   NA 1937.29      40
## 30  1937   30   NA 1937.30      40
## 31  1937   31   NA 1937.31      40
## 32  1937   32   NA 1937.32      40
## 33  1937   33   NA 1937.33      40
## 34  1937   34   NA 1937.34      40
## 35  1937   35   NA 1937.35      40
## 36  1937   36   NA 1937.36      40
## 37  1937   37   NA 1937.37      40
## 38  1937   38   NA 1937.38      40
## 39  1937   39   NA 1937.39      40
## 40  1937   40   NA 1937.40      40
## 41  1937   41   NA 1937.41      40
## 42  1937   42   NA 1937.42      40
## 43  1937   43   NA 1937.43      40
## 44  1937   44    3 1937.44      40
## 45  1937   45    5 1937.45      40
## 46  1937   46    8 1937.46      40
## 47  1937   47    1 1937.47      40
## 48  1937   48    7 1937.48      40
## 49  1937   49    1 1937.49      40
## 50  1937   50    6 1937.50      40
## 51  1937   51    9 1937.51      40
## 52  1937   52   NA 1937.52      40
## 53  1938   01    2 1938.01     835
## 54  1938   02   10 1938.02     835
## 55  1938   03   14 1938.03     835
## 56  1938   04   22 1938.04     835
## 57  1938   05   55 1938.05     835
## 58  1938   06    6 1938.06     835
## 59  1938   07    9 1938.07     835
## 60  1938   08   14 1938.08     835
## 61  1938   09   NA 1938.09     835
## 62  1938   10   10 1938.10     835
## 63  1938   11    7 1938.11     835
## 64  1938   12   19 1938.12     835
## 65  1938   13   28 1938.13     835
## 66  1938   14   25 1938.14     835
## 67  1938   15    2 1938.15     835
## 68  1938   16   25 1938.16     835
## 69  1938   17   24 1938.17     835
## 70  1938   18   17 1938.18     835
## 71  1938   19   32 1938.19     835
## 72  1938   20   19 1938.20     835
## 73  1938   21   33 1938.21     835
## 74  1938   22   20 1938.22     835
## 75  1938   23   11 1938.23     835
## 76  1938   24   26 1938.24     835
## 77  1938   25   44 1938.25     835
## 78  1938   26   17 1938.26     835
## 79  1938   27   16 1938.27     835
## 80  1938   28   NA 1938.28     835
## 81  1938   29   19 1938.29     835
## 82  1938   30    9 1938.30     835
## 83  1938   31   17 1938.31     835
## 84  1938   32   12 1938.32     835
## 85  1938   33   NA 1938.33     835
## 86  1938   34   50 1938.34     835
## 87  1938   35    0 1938.35     835
## 88  1938   36   14 1938.36     835
## 89  1938   37   NA 1938.37     835
## 90  1938   38   30 1938.38     835
## 91  1938   39   13 1938.39     835
## 92  1938   40   13 1938.40     835
## 93  1938   41   11 1938.41     835
## 94  1938   42   35 1938.42     835
## 95  1938   43   11 1938.43     835
## 96  1938   44    0 1938.44     835
## 97  1938   45    4 1938.45     835
## 98  1938   46   17 1938.46     835
## 99  1938   47    9 1938.47     835
## 100 1938   48    0 1938.48     835
## 101 1938   49   21 1938.49     835
## 102 1938   50    0 1938.50     835
## 103 1938   51   34 1938.51     835
## 104 1938   52    9 1938.52     835
## 105 1939   01    9 1939.01    1176
## 106 1939   02   14 1939.02    1176

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

plot of chunk unnamed-chunk-1


### same plots plotted using 'plot' ###
par(mfrow = c(2, 1), mar = c(4, 4, 1, 1))
plot(WC.FL$YR.WK, WC.FL$CASE, type = "p", pch = ".", xlab = "WEEK")
plot(WC.FL$YEAR, WC.FL$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 ###
WC.FLc <- WC.FL[complete.cases(WC.FL), ]
WC.FLcs <- as.array(split(WC.FLc, WC.FLc$YEAR))

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