Question

I am looking for seasonal summaries of daily time series. I have daily time series climate data from many stations in the format as below.

head(df)
        Date     ID  Ele Aspect  Tmin  Tmax Prcp
1 1970-01-01 crb200 1283   West -13.3  -2.8    0
2 1970-01-02 crb200 1283   West -12.2  -5.6    1
3 1970-01-03 crb200 1283   West -15.0  -6.1    0
4 1970-01-04 crb200 1283   West -20.0 -10.0    0
5 1970-01-05 crb200 1283   West -19.4  -6.7    0
6 1970-01-06 crb200 1283   West -16.1  -5.6    0

The long data frame df is here.This is one year data as example for seasonal summaries.

I can summarize this to monthly (and yearly) using ddply as below codes.

df$Date<-as.Date(df$Date, format="%Y-%m-%d",tz='GMT')
df.m<-ddply(df,.(Date=format(Date,"%Y-%b")),here(summarise),
               ID=(df[2,2]),
               Ele=(df[2,4]),
               Aspect=(df[2,5]),
               Prcp.m = sum(Prcp, na.rm=F),
               Tmin.m = mean(Tmin, na.rm=F),
               Tmax.m = mean(Tmax, na.rm=F))
print(summary(df.m))

I am trying to perform similar summaries for each season of every year. i e i want total precipitation and mean minimum and maximum temperatures of 4 seasons of each year.

Is there a way i can achieve this using ddply ? If not how i can do that ? Thank you.

Was it helpful?

Solution 2

I figured out that with help of zoo package and use of ddply this can be achieved. the code is as below. this is continuation of previous code.

df.s<-ddply(df.m,.(sea=(format(as.yearqtr(as.yearmon(df$Date, "%Y-%b") + 1/12)))),here(summarise),
                   ID=(df[2,2]),
                   Ele=(df[2,4]),
                   Aspect=(df[2,5]),
                   Prcp.sea = sum(Prcp.m, na.rm=F),
                   Tmin.sea = mean(Tmin.m, na.rm=F),
                   Tmax.sea = mean(Tmax.m, na.rm=F))
print(summary(dataframe.s))

OTHER TIPS

How about a lookup table - change season.N to season.Sif your data is southern hemisphere:

seasons<-data.frame(month=c("Dec","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov"),
           season.N=rep(c("Winter","Spring","Summer","Fall"),each=3),
           season.S=rep(c("Summer","Fall","Winter","Spring"),each=3))

df.s<-ddply(df,.(season=seasons[seasons$month==format(Date,"%b"),"season.N"]),here(summarise),
            ID=(df[2,2]),
            Ele=(df[2,4]),
            Aspect=(df[2,5]),
            Prcp.m = sum(Prcp, na.rm=F),
            Tmin.m = mean(Tmin, na.rm=F),
            Tmax.m = mean(Tmax, na.rm=F))

You can also used Seas package to get the results.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top