agg_table: Aggregates a data frame to a larger time period

View source: R/ff_agg_table.R

agg_tableR Documentation

Aggregates a data frame to a larger time period

Description

Aggregates a data frame to a larger time period

Usage

agg_table(
  x,
  col_name,
  fun,
  period,
  out_name = NULL,
  allow_na = 0,
  start_month = 1,
  end_month = 12
)

Arguments

x

data frame or tibble with class Date or POSIX* in the first column.

col_name

string with column(s) name(s) to aggregate.

fun

string with supported aggregation function name (one per col_name): mean, min, max, sum, last or first.

period

string with the aggregation time-step: hourly, daily, monthly, annually or climatic. NOTE: the climatic option returns the all series annual statistics (fun).

out_name

optional. String with the output column(s) name(s). Default values coerce the original name plus the fun argument (e.g.: tair_max).

allow_na

optional. Numeric value with the maximum allowed number of NA_real_ values. By default the function will not tolerate any NA_real_ (and will return NA_real_ instead).

start_month

optional. Numeric value defining the first month of the annual period (it just make sense if period is either annually or climatic). Default sets to 1 (January). NOTE: keep in mind that if you choose climatic as period you have to round off a complete year (e.g.: ..., start_month = 6, end_month = 5, ...)

end_month

optional. Numeric value defining the last month of the annual period (it just make sense if period is either annually or climatic). Default sets to 12 (December). NOTE: keep in mind that if you choose climatic as period you have to round off a complete year (e.g.: ..., start_month = 6, end_month = 5, ...)

Value

A data frame with the Date and the aggregated variable(s).

Examples


# set path to file
path <- system.file('extdata', 'snih_qd_guido.xlsx',
         package = 'hydrotoolbox')

# read and load daily streamflow with default column name
guido_qd <- read_snih(path = path, by = 'day', out_name = 'q(m3/s)')

# aggregate daily to monthly discharge
guido_q_month <- agg_table(x = guido_qd, col_name = 'q(m3/s)',
                          fun = 'mean', period = 'monthly',
                          out_name = 'qm(m3/s)')

# suppose that we are interested on getting the annual maximum
# daily mean discharge for every hydrological year (since this
# station is located at the Mendoza River Basin ~32.9º S, we will
# consider that annual period starts on July)
guido_q_annual <- agg_table(x = guido_qd, col_name = 'q(m3/s)',
                            fun = 'max', period = 'annually',
                            out_name = 'qmax(m3/s)',
                            start_month = 7, end_month = 6)

# now we want the mean, maximum and minimum monthly discharges
guido_q_stats <- agg_table(x = guido_qd, col_name = rep('q(m3/s)', 3),
                           fun = c('mean', 'max', 'min'),
                           period = 'monthly')



hydrotoolbox documentation built on April 14, 2023, 12:34 a.m.