Domanda

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

È stato utile?

Soluzione

I don't really know what's wrong with your data frame because if I create a new data frame as:

> dd = data.frame(year = as.factor(dat$year), rain = dat$rain)

ddply works. I am quite sure that there is a sort of bug with plyr when the data frame has a POSIXlt/ct date as column, in fact if you remove yearmoda column the error disappears...

UPDATE It's not a "sort of" bug, it's a real bug: https://github.com/hadley/plyr/issues/159

Altri suggerimenti

I don't know if that solves your problem as I currently cannot run your minimum working example, but if you want to try something else than plyr the package doBy provides a function summaryBy.

rain <- summaryBy(rain ~ year, data = dat, FUN = sum)

From there plotting should be straightforward.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top