Learn R Programming

hydroTSM (version 0.3-5)

seasonalfunction: Seasonal Function

Description

Generic function for applying any R function to a zoo object, in order to obtain 4 representative seasonal values.

Usage

seasonalfunction(x, ...)

## S3 method for class 'default': seasonalfunction(x, FUN, na.rm = TRUE, type="default", ...)

## S3 method for class 'zoo': seasonalfunction(x, FUN, na.rm = TRUE, type="default", ...)

## S3 method for class 'data.frame': seasonalfunction(x, FUN, na.rm = TRUE, type="default", dates, date.fmt = "\%Y-\%m-\%d", out.type = "data.frame", verbose = TRUE, ...) ## S3 method for class 'matrix': seasonalfunction(x, FUN, na.rm = TRUE, type="default", dates, date.fmt = "\%Y-\%m-\%d", out.type = "data.frame", verbose = TRUE, ...)

Arguments

x
zoo, data.frame or matrix object, with daily or monthly time series. Measurements at several gauging stations can be stored in a data.frame of matrix object, and in that case, each column of x represent the time series measured in each gaugi
FUN
Function that will be applied to ALL the values in x belonging to each one of the 4 weather seasons (e.g., FUN can be some of mean, max, min, sd).
na.rm
Logical. Should missing values be removed before the computations? -) TRUE : the monthly values are computed considering only those values in x different from NA (very important when FUN=sum) -) FALSE
type
character, indicating which weather seasons will be used for computing the output. Possible values are: -) default => "winter"= Dec, Jan, Feb; "spring"= Mar, Apr, May; "summer"=Jun, Jul, Aug; "autumn"= Sep, Oct, Nov -)
dates
numeric, factor, Date indicating how to obtain the dates. If dates is a number, it indicates the index of the column in x that stores the dates If dates is a factor, it is converted into Date class, by using the da
date.fmt
Character indicating the format in which the dates are stored in dates, e.g. %Y-%m-%d. See format in as.Date. ONLY required when class(dates)=="factor"
out.type
Character defining the desired type of output. Valid values are: -) data.frame: a data.frame, with 4 columns representing the weather seasons, and as many rows as stations are included in x -) db : a data.frame,
verbose
Logical; if TRUE, progress messages are printed
...
further arguments passed to or from other methods

Warning

The FUN value for the winter season (DJF) is computed considering the consecutive months of December, January and February. Therefore, if x starts in January and ends in December of any year, the winter value of the first year is computed considering only the January and February value of that year, whereas the December value of the first year is used to compute the winter value of the next year.

See Also

dm2seasonal, time2season, monthlyfunction, annualfunction, extract

Examples

Run this code
## Loading the SanMartino precipitation data
data(SanMartinoPPts)
x <- SanMartinoPPts

# Amount of years
nyears <- yip(from=start(x), to=end(x), out.type="nmbr")

## Mean annual precipitation.
# It is necessary to divide by the amount of years to obtain the mean annual value, 
# otherwise it will give the total precipitation for all the 70 years
seasonalfunction(x, FUN=sum, na.rm=TRUE) / nyears

#####################
### verification ####
# Mean winter (DJF) value
sum( extractzoo(x, trgt="DJF") ) / nyears

# Mean spring (MAM) value
sum( extractzoo(x, trgt="MAM") ) / nyears

# Mean summer (JJA) value
sum( extractzoo(x, trgt="JJA") ) / nyears

# Mean autumn (SON) value
sum( extractzoo(x, trgt="SON") ) / nyears

############
############
## Loading the monthly time series of precipitation within the Ebro River basin.
data(EbroPPtsMonthly)
x <- EbroPPtsMonthly

## Winter (DJF) mean values of precipitation for the first 3 stations 
## in 'EbroPPtsMonthly' (its first column stores the dates)
seasonalfunction(x[,1:4], FUN=mean, dates=1)

## The same previous example, but using a zoo object
dates <- as.Date(x[,1]) # dates of the zoo object
z     <- zoo(x[ ,2:ncol(x)], dates) # zoo creation
seasonalfunction(z, FUN=mean)

Run the code above in your browser using DataLab