Adds a column of seasons identifiers to a data frame with a column of dates called 'Date'. The length of seasons,
in months, is provided using the seasons_length
argument. As seasons are grouped by months the length of the seasons
must be divisible into 12 with one of the following season lengths: 1, 2, 3, 4, 6, or 12 months. The start of the first season
coincides with the start month of each year; 'Jan-Jun' for 6-month seasons starting with calendar years or 'Dec-Feb' for 3-month
seasons starting with water year starting in December.
add_seasons(
data,
dates = Date,
station_number,
water_year_start = 1,
seasons_length
)
Data frame of daily data that contains columns of dates, flow values, and (optional) groups (e.g. station numbers).
Leave blank or set to NULL
if using station_number
argument.
Name of column in data
that contains dates formatted YYYY-MM-DD. Only required if dates column name is not
'Date' (default). Leave blank or set to NULL
if using station_number
argument.
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.
Numeric value indicating the month (1
through 12
) of the start of water year for
analysis. Default 1
.
Numeric value indicating the desired length of seasons in months, divisible into 12. Required.
A tibble data frame of the source data with additional column:
season identifier labelled by the start and end month of the season
# Run if HYDAT database has been downloaded (using tidyhydat::download_hydat())
if (file.exists(tidyhydat::hy_downloaded_db())) {
# Add a column with four annual seasons starting in January
add_seasons(station_number = "08NM116",
seasons_length = 4)
# Add a column with two annual seasons (of 6 months length) starting in October
add_seasons(station_number = "08NM116",
water_year_start = 10,
seasons_length = 6)
}
#> # A tibble: 23,079 × 6
#> STATION_NUMBER Date Parameter Value Symbol Season
#> <chr> <date> <chr> <dbl> <chr> <fct>
#> 1 08NM116 1949-04-01 Flow 1.13 E Apr-Sep
#> 2 08NM116 1949-04-02 Flow 1.53 E Apr-Sep
#> 3 08NM116 1949-04-03 Flow 2.07 E Apr-Sep
#> 4 08NM116 1949-04-04 Flow 2.07 E Apr-Sep
#> 5 08NM116 1949-04-05 Flow 2.21 E Apr-Sep
#> 6 08NM116 1949-04-06 Flow 2.21 NA Apr-Sep
#> 7 08NM116 1949-04-07 Flow 2.27 NA Apr-Sep
#> 8 08NM116 1949-04-08 Flow 2.35 NA Apr-Sep
#> 9 08NM116 1949-04-09 Flow 2.78 NA Apr-Sep
#> 10 08NM116 1949-04-10 Flow 2.94 NA Apr-Sep
#> # … with 23,069 more rows