R/add_rolling_means.R
add_rolling_means.Rd
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" )
data | Data frame of daily data that contains columns of dates, flow values, and (optional) groups (e.g. station numbers).
Leave blank if using |
---|---|
dates | Name of column in |
values | Name of column in |
groups | Name of column in |
station_number | Character string vector of seven digit Water Survey of Canada station numbers (e.g. |
roll_days | Numeric values of the number of days to apply a rolling mean. Default |
roll_align | Character string identifying the direction of the rolling mean from the specified date, either by the first
( |
A data frame of the source data with an additional column(s):
rolling means of the n-day flow values of the designated date and adjacent dates, direction of mean specified by roll_align
rolling means of the 3-day flow values of the designated date and previous 2 days (roll_align = "right")
rolling means of the 7-day flow values of the designated date and previous 6 days (roll_align = "right")
rolling means of the 30-day flow values of the designated date and previous 29 days (roll_align = "right")
# 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