Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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