Add columns of CalendarYear (YYYY), Month (MM), MonthName (e.g. 'Jan'), WaterYear (YYYY), and DayofYear (1-365 or 366; of WaterYear); to a data frame with a column of dates called 'Date'. Water years are designated by the year in which they end. For example, Water Year 1999 (starting Oct) is from 1 Oct 1998 (DayofYear 1) to 30 Sep 1999 (DayofYear 365)).

add_date_variables(data, dates = Date, station_number, water_year_start = 1)

Arguments

data

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.

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 or set to NULL 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.

water_year_start

Numeric value indicating the month (1 through 12) of the start of water year for analysis. Default 1.

Value

A tibble data frame of the source data with additional columns:

CalendarYear

calendar year

Month

numeric month (1 to 12)

MonthName

month abbreviation (Jan-Dec)

WaterYear

year starting from the selected month start, water_year_start

DayofYear

day of the year from the selected month start (1-365 or 366)

Examples

# Run if HYDAT database has been downloaded (using tidyhydat::download_hydat())
if (file.exists(tidyhydat::hy_downloaded_db())) {

# Add date variables using calendar years
add_date_variables(station_number = "08NM116")

# Add date variables using water years starting in August
add_date_variables(station_number = "08NM116", 
                   water_year_start = 8)
                   
}
#> # A tibble: 23,079 × 10
#>    STATI…¹ Date       Param…² Value Symbol Calen…³ Month Month…⁴ Water…⁵ Dayof…⁶
#>    <chr>   <date>     <chr>   <dbl> <chr>    <dbl> <dbl> <fct>     <dbl>   <dbl>
#>  1 08NM116 1949-04-01 Flow     1.13 E         1949     4 Apr        1949     244
#>  2 08NM116 1949-04-02 Flow     1.53 E         1949     4 Apr        1949     245
#>  3 08NM116 1949-04-03 Flow     2.07 E         1949     4 Apr        1949     246
#>  4 08NM116 1949-04-04 Flow     2.07 E         1949     4 Apr        1949     247
#>  5 08NM116 1949-04-05 Flow     2.21 E         1949     4 Apr        1949     248
#>  6 08NM116 1949-04-06 Flow     2.21 NA        1949     4 Apr        1949     249
#>  7 08NM116 1949-04-07 Flow     2.27 NA        1949     4 Apr        1949     250
#>  8 08NM116 1949-04-08 Flow     2.35 NA        1949     4 Apr        1949     251
#>  9 08NM116 1949-04-09 Flow     2.78 NA        1949     4 Apr        1949     252
#> 10 08NM116 1949-04-10 Flow     2.94 NA        1949     4 Apr        1949     253
#> # … with 23,069 more rows, and abbreviated variable names ¹​STATION_NUMBER,
#> #   ²​Parameter, ³​CalendarYear, ⁴​MonthName, ⁵​WaterYear, ⁶​DayofYear