Skip to content

Instantly share code, notes, and snippets.

@gavinsimpson
Created May 12, 2014 19:24
Show Gist options
  • Save gavinsimpson/b52f6d375f57d539818b to your computer and use it in GitHub Desktop.
Save gavinsimpson/b52f6d375f57d539818b to your computer and use it in GitHub Desktop.
R code to load and process the monthly data set from the Central England Temperature time series
## read in the CET data
CET <- url("http://www.metoffice.gov.uk/hadobs/hadcet/cetml1659on.dat")
cet <- read.table(CET, sep = "", skip = 6, header = TRUE,
fill = TRUE, na.string = c(-99.99, -99.9))
names(cet) <- c(month.abb, "Annual") ## fix up df names
## remove last row of incomplete data
cet <- cet[-nrow(cet), ]
## get rid of the annual too - store for plotting
rn <- as.numeric(rownames(cet))
Years <- rn[1]:rn[length(rn)]
annCET <- data.frame(Temperature = cet[, ncol(cet)],
Year = Years)
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)),
Date = as.Date(paste(Year, Month, "15", sep = "-"),
format = "%Y-%b-%d"))
## sort into temporal order
cet <- cet[with(cet, order(Date)), ]
## Add in a Time variable
cet <- transform(cet, Time = as.numeric(Date) / 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment