Last active
November 21, 2015 23:02
-
-
Save gavinsimpson/526ae3e1b02d333d85e4 to your computer and use it in GitHub Desktop.
A simple function to load and process the CET data into a nice format for modelling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## read in the CET data | |
`loadCET` <- function() { | |
CET <- url("http://www.metoffice.gov.uk/hadobs/hadcet/cetml1659on.dat") | |
on.exit(close(CET)) | |
cet <- read.table(CET, sep = "", skip = 6, header = TRUE, | |
fill = TRUE, na.string = c(-99.99, -99.9)) | |
names(cet) <- c(month.abb, "Annual") | |
## remove last row of incomplete data | |
cet <- cet[-nrow(cet), ] # FIXME: this removes the last row regardless | |
## get rid of the annual too - store for plotting | |
rn <- as.numeric(rownames(cet)) | |
Years <- rn[1]:rn[length(rn)] | |
cet <- cet[, -ncol(cet)] | |
## stack the data | |
cet <- stack(cet)[,2:1] | |
names(cet) <- c("Month","Temperature") | |
## add in Year and nMonth for numeric month and a proper Date class | |
cet <- transform(cet, | |
Year = (Year <- rep(Years, times = 12)), | |
nMonth = rep(1:12, each = length(Years))) | |
## sort into temporal order | |
cet <- cet[with(cet, order(Year, Month)), ] | |
cet | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment