Adds selected n-day rolling means to a daily streamflow data set. Based on selected n-days and alignment, the rolling mean for a given day is obtained by averaging the adjacent dates of daily mean values. For example, rolling days of '7' and 'right' alignment would obtain a mean of the given and previous 6 days of daily mean flow.

add_rolling_means(
  data,
  dates = Date,
  values = Value,
  groups = STATION_NUMBER,
  station_number,
  roll_days = c(3, 7, 30),
  roll_align = "right"
)

Arguments

data

Data frame of daily data that contains columns of dates, flow values, and (optional) groups (e.g. station numbers). Leave blank if using station_number argument.

dates

Name of column in data that contains dates formatted YYYY-MM-DD. Only required if dates column name is not 'Date' (default). Leave blank if using station_number argument.

values

Name of column in data that contains numeric flow values, in units of cubic metres per second. Only required if values column name is not 'Value' (default). Leave blank if using station_number argument.

groups

Name of column in data that contains unique identifiers for different data sets, if applicable. Only required if groups column name is not 'STATION_NUMBER'. Function will automatically group by a column named 'STATION_NUMBER' if present. Remove the 'STATION_NUMBER' column beforehand to remove this grouping. Leave blank if using station_number argument.

station_number

Character string vector of seven digit Water Survey of Canada station numbers (e.g. "08NM116") of which to extract daily streamflow data from a HYDAT database. Requires tidyhydat package and a HYDAT database. Leave blank if using data argument.

roll_days

Numeric values of the number of days to apply a rolling mean. Default c(3,7,30).

roll_align

Character string identifying the direction of the rolling mean from the specified date, either by the first ('left'), last ('right'), or middle ('center') day of the rolling n-day group of observations. Default 'right'.

Value

A data frame of the source data with an additional column(s):

QnDay

rolling means of the n-day flow values of the designated date and adjacent dates, direction of mean specified by roll_align

Default additional columns:
Q3Day

rolling means of the 3-day flow values of the designated date and previous 2 days (roll_align = "right")

Q7Day

rolling means of the 7-day flow values of the designated date and previous 6 days (roll_align = "right")

Q30Day

rolling means of the 30-day flow values of the designated date and previous 29 days (roll_align = "right")

Examples

# Run if HYDAT database has been downloaded (using tidyhydat::download_hydat()) if (file.exists(tidyhydat::hy_downloaded_db())) { # Add default 3, 7, and 30-day rolling mean columns, with "right" alignment add_rolling_means(station_number = "08NM116") # Add custom 5 and 10-day rolling mean columns add_rolling_means(station_number = "08NM116", roll_days = c(5,10)) # Add default 3, 7, and 30-day rolling mean columns, with "left" alignment add_rolling_means(station_number = "08NM116", roll_align = "left") }
#> # A tibble: 21,983 x 8 #> STATION_NUMBER Date Parameter Value Symbol Q3Day Q7Day Q30Day #> <chr> <date> <chr> <dbl> <chr> <dbl> <dbl> <dbl> #> 1 08NM116 1949-04-01 Flow 1.13 E 1.58 1.93 7.51 #> 2 08NM116 1949-04-02 Flow 1.53 E 1.89 2.10 7.95 #> 3 08NM116 1949-04-03 Flow 2.07 E 2.12 2.28 8.37 #> 4 08NM116 1949-04-04 Flow 2.07 E 2.16 2.40 8.75 #> 5 08NM116 1949-04-05 Flow 2.21 E 2.23 2.88 9.08 #> 6 08NM116 1949-04-06 Flow 2.21 NA 2.28 3.24 9.40 #> 7 08NM116 1949-04-07 Flow 2.27 NA 2.47 3.61 9.72 #> 8 08NM116 1949-04-08 Flow 2.35 NA 2.69 3.85 10.1 #> 9 08NM116 1949-04-09 Flow 2.78 NA 3.70 4.20 10.7 #> 10 08NM116 1949-04-10 Flow 2.94 NA 4.36 4.78 11.4 #> # ... with 21,973 more rows