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 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.

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: 21,983 x 10 #> STATION_NUMBER Date Parameter Value Symbol CalendarYear Month MonthName #> <chr> <date> <chr> <dbl> <chr> <dbl> <dbl> <fct> #> 1 08NM116 1949-04-01 Flow 1.13 E 1949 4 Apr #> 2 08NM116 1949-04-02 Flow 1.53 E 1949 4 Apr #> 3 08NM116 1949-04-03 Flow 2.07 E 1949 4 Apr #> 4 08NM116 1949-04-04 Flow 2.07 E 1949 4 Apr #> 5 08NM116 1949-04-05 Flow 2.21 E 1949 4 Apr #> 6 08NM116 1949-04-06 Flow 2.21 NA 1949 4 Apr #> 7 08NM116 1949-04-07 Flow 2.27 NA 1949 4 Apr #> 8 08NM116 1949-04-08 Flow 2.35 NA 1949 4 Apr #> 9 08NM116 1949-04-09 Flow 2.78 NA 1949 4 Apr #> 10 08NM116 1949-04-10 Flow 2.94 NA 1949 4 Apr #> # ... with 21,973 more rows, and 2 more variables: WaterYear <dbl>, #> # DayofYear <dbl>