View source: R/annualfunction.R
annualfunction | R Documentation |
Generic function for obtaining a SINGLE annual value of a zoo object, by applying any R function to ALL the values in x
belonging to the same year, and then applying the same function to ALL the previously computed annual values (e.g., for computing the average annual precipitation or the mean annual streamflow of a long-term time series).
annualfunction(x, FUN, na.rm = TRUE, ...)
## Default S3 method:
annualfunction(x, FUN, na.rm = TRUE, ...)
## S3 method for class 'zoo'
annualfunction(x, FUN, na.rm = TRUE, ...)
## S3 method for class 'data.frame'
annualfunction(x, FUN, na.rm = TRUE, dates=1, date.fmt = "%Y-%m-%d",
verbose = TRUE, ...)
## S3 method for class 'matrix'
annualfunction(x, FUN, na.rm = TRUE, dates=1, date.fmt = "%Y-%m-%d",
verbose = TRUE, ...)
x |
zoo, xts, data.frame or matrix object, with daily/monthly/annual time series. |
FUN |
Function that will be used to compute the final annual value (e.g., |
na.rm |
Logical. Should missing values be removed?. |
dates |
numeric, factor or Date object indicating how to obtain the dates corresponding to each gauging station. |
date.fmt |
character indicating the format in which the dates are stored in dates, e.g. %Y-%m-%d. See |
verbose |
Logical; if TRUE, progress messages are printed. |
... |
further arguments passed to or from other methods. |
When x
is a time series, a single annual value is returned.
For a data frame, a named vector with the appropriate method being applied column by column.
FUN
is first applied to all the values of x
belonging to the same year and then it is applied to all the previously computed annual values to get the final result.
Its result will depend on the sampling frequency of x
and the type of function provided by FUN
(special attention have to be put when FUN=sum
)
Mauricio Zambrano-Bigiarini, mzb.devel@gmail
monthlyfunction
, daily2annual
, monthly2annual
, yip
## Loading the SanMartino daily precipitation data (1921-1990)
data(SanMartinoPPts)
x <- SanMartinoPPts
# Amount of years in 'x' (needed for computing the average)
nyears <- length( seq(from=time(x[1]), to=time(x[length(x)]), by="years") )
## Average annual precipitation for the 70 years period.
# It is necessary to divide by the amount of years to obtain the average annual value,
# otherwise it will give the total precipitation for all the 70 years.
annualfunction(x, FUN=sum, na.rm=TRUE) / nyears
#####################
### verification ####
# Daily to annual
a <- daily2annual(x, FUN=sum, na.rm=TRUE)
# Mean annual value
mean(a)
##############################
##############################
## Loading the monthly time series of precipitation within the Ebro River basin.
data(EbroPPtsMonthly)
x <- EbroPPtsMonthly
## Dates of 'x'
dates <- as.Date(x[,1])
## Computation of the average annual precipitation
## Not run:
## Transforming 'x' into a zoo object
z <- zoo( x[, 2:ncol(x)], dates)
# Amount of years in 'x' (needed for computing the average)
nyears <- yip(from=start(z), to=end(z), out.type="nmbr" )
## Average annual precipitation, for the first 5 stations in 'x'
annualfunction(z[ ,1:5], FUN=sum)/nyears
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.