add_date_variables: Add year, month, and day of year variable columns to daily...

View source: R/add_date_variables.R

add_date_variablesR Documentation

Add year, month, and day of year variable columns to daily flows

Description

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

Usage

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)
                   
}

fasstr documentation built on Sept. 30, 2024, 9:24 a.m.