valid_year_month: Ensure that years and months are within a certain time span

Description Usage Arguments Details Value Examples

View source: R/utils.R

Description

Ensure that years and months are within a certain time span

Usage

1
valid_year_month(years, months, begin = "1970-01-01", end = Sys.Date())

Arguments

years

a numeric vector of years

months

a numeric vector of months

begin

the earliest valid date, defaults to the UNIX epoch

end

the most recent valid date, defaults to today

Details

Often, a data source will begin and end at known points in time. At the same time, many data sources are divided into monthly archives. Given a set of years and months, any combination of which should be considered valid, this function will return a data.frame in which each row is one of those valid year-month pairs. Further, if the optional begin and end arguments are specified, the rows will be filter to lie within that time interval. Furthermore, the first and last day of each month are computed.

Value

a data.frame with four variables: year, month, month_begin (the first day of the month), and month_end (the last day of the month).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
valid_year_month(years = 1999:2001, months = c(1:3, 7))

# Mets in the World Series since the UNIX epoch
mets_ws <- c(1969, 1973, 1986, 2000, 2015)
valid_year_month(years = mets_ws, months = 10)

# Mets in the World Series during the Clinton administration
if (require(ggplot2)) {
  clinton <- filter(presidential, name == "Clinton")
  valid_year_month(years = mets_ws, months = 10,
    begin = clinton$start, end = clinton$end)
}

etl documentation built on Sept. 28, 2017, 1:06 a.m.