Question

I have daily time series data (date Vs rainfall). I would like to list maximum daily rainfall for each year (27 years). With which.max i can return only one daily max rainfall value for the whole (27 years) time series. Can anybody suggest a better way of doing this? I tried:

> head(dt)

        date pcp

1 1987-01-01  2

2 1987-01-02  0.6

3 1987-01-03  0

.

.

.

. 2013-12-31  0.6

> dates <- as.Date(dt$date,"%Y/%m/%d")

> rain <- zoo(dt$pcp, dates)

> ndx = which.max(rain) 

> dmax = rain[ndx,]

> dmax

1999-04-05 
     269.2
Was it helpful?

Solution

aggregate(pcp ~ format(as.Date(date), "%Y"), dt, max)

OTHER TIPS

I woudl suggest this, but I'm sure something more elegant is out there.

dt$yrs <- 1900 + as.POSIXlt(dt$date)$year
dat    <- data.frame(year= sort(unique(dt$yrs)), prc= numeric(length(unique(dt$yrs))))
for(i in 1:nrow(dat)){
  dat$prc[i] <- dt$pcp[which.max(dt[dt$year == dat$year[i], "pcp"])]
}

ddply really shines at this.

Solution using ddply in Plyr

rain <- data.frame(date=c("1987-01-01", "1987-01-02", "1987-01-03", "1988-10-01", "1988-11-02", "1988-11-01"), 
              pcp=c(2, 0.6, 1, 2.7, 1.1, 1.3)
           )

rain$Year <- 1900+as.POSIXlt(rain$date)$year
library(plyr)
ddply(rain, .(Year), summarize, MaxPcpForYear=max(pcp))

#  Year MaxPcpForYear
# 1 1987           2.0
# 2 1988           2.7
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top