I am using the code below to get cumulative rainfall by year and plot them as lines for each year but I get the following error when I run the ddply
function from the plyr
package.
Error: 'names' attribute [9] must be the same length as the vector [3]
library(plyr)
# Setting work directory
setwd("d:\\ClimData")
# Reading and reformatting raw data downloaded from NCDC
dat<-read.table("CDO2812586929956.txt",header=F,skip=1)
colnames(dat)<-c("stn","wban","yearmoda","temp","tempc","dewp","dewpc","slp","slpc","stp","stpc","visib","visibc","wdsp","wdspc","mxspd","gust","maxtemp","mintemp","prcp","sndp","frshtt")
dat$yearmoda <- strptime(dat$yearmoda,format="%Y%m%d")
dat$prcp <- as.character(dat$prcp)
dat$prcp1 <-as.numeric(substr(dat$prcp,1,4))
dat$prcpflag <- substr(dat$prcp,5,5)
dat$rain <- dat$prcp1*25.4
dat$rain[dat$rain > 1000 ] <- NA
dat$year <- as.numeric(format(dat$yearmoda,"%Y"))
dat$month <- as.numeric(format(dat$yearmoda,"%m"))
dat$day <- as.numeric(format(dat$yearmoda,"%d"))
# Getting cumulative sum of rain/year
dat <- ddply (dat,.(year), transform, cumRainfall = cumsum (rain))
Hopefull someone can point out where I went wrong.
The input file is at the link below.
https://dl.dropboxusercontent.com/u/81632971/CDO2812586929956.txt