R code to plot CET daily

As a programmer who has learnt & used perhaps 20-30 programming languages (I even created my own “language” at one one for a particular application), when I heard some other skeptics talking about using ‘R’ I made the mistake of thinking it would be a very simple task to get up to speed with ‘R’

I was wrong R … isn’t a language

It’s the expression I find myself saying when trying to do even the simplest thing Rrrrr … If that hasn’t deterred you, then you can download the code here:
http://cran.r-project.org/bin/windows/base/
And there’s an introduction here:
http://cran.r-project.org/doc/manuals/R-intro.html
And quickly here are a few “hello world” examples. Copy and paste these into the window that says “R console”:
Show Hello World

cat(‘Hello, world!n’)

Print 2+2

2+2

Set a variable and print

x <- 2+2
x

Plot squares

x <- 1:9
x #display x
y <- c(0, 1 ,4 ,9 ,16 ,9 ,4 ,1 ,0)
y #display y
plot (x, y, type=’l’)

Plot CET daily
Notes:

  1. set “c:/r/temp.dat” to an appropriate directory on your own computer.
  2. First time remove the “#” from the second & third line so as to download the  file (I’ve commented these out, to remind you that it is only necessary to download this file once as it is then stored on your computer.)
  3. This code plots three graphs. If you just copy and paste, they will display in quick succession.

dest=”c:/r/temp.dat”
#loc=”http://www.metoffice.gov.uk/hadobs/hadcet/cetdl1772on.dat”
#download.file(loc,dest)
work=read.table(dest,fill=TRUE)
names(work)[1:2] = c(“year”,”day”)
 dim(work)
# [1] 7502   14
N=nrow(work)
Data= data.frame( year= rep(work$year,12), day=rep(work$day,12),
  month=  c( t( array(rep(1:12,N),dim=c(12,N))))   , tem= c(t(t(work[,3:14]) )))
Data=Data[order(Data$year,Data$month,Data$day),]
Data=Data[,c(1,3,2,4)]
Data$dd=as.Date( paste(Data$year,Data$month,Data$day,sep=”-“))
Data=Data[!is.na(Data$dd),]
Data$julian=julian(Data$dd)
Data$tem[Data$tem== -999]=NA
#plot raw daily temperature
plot(Data$julian, Data$tem, type=’l’)
#plot daily temperature smoothed over a year
fyear <- rep(1/365, 365)
plot(Data$julian, filter(Data$tem/10, fyear, sides=1), type=’l’)
#plot daily temperature smoothed over a decade
fdecade <- rep(1/3650, 3650)

plot(Data$julian, filter(Data$tem/10, fdecade, sides=1), type=’l’)

Finally thanks to Steve McIntyre for original code

This entry was posted in Climate, computing and tagged . Bookmark the permalink.

2 Responses to R code to plot CET daily

  1. Richard Mallett says:

    Please let me know what you find out. Since the Hadley Centre gives me monthly and seasonal averages that I can import into Excel 2013, I haven’t bothered with daily temperatures.

  2. catweazle666 says:

    Hmmm….
    Looks like it’s got a lot of “magic numbers” in it.

Comments are closed.