Skip to content

Instantly share code, notes, and snippets.

@gavinsimpson

gavinsimpson/loadCET

Last active Nov 21, 2015
Embed
What would you like to do?
A simple function to load and process the CET data into a nice format for modelling
## 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