time_shift adds days, weeks, months, quarters or years to dates. date_month, date_quarter and date_year return the first day of the period and are useful for customized aggregation of data frames. For standard aggregation, use ts_frequency().

time_shift(x, by = NULL)

date_month(x)

date_quarter(x)

date_year(x)

Arguments

x

Date or POSIXct. If POSIXct, it is converted into Date.

by

passed on to base::seq.Date(). Argument can be specified in several ways: - A number, taken to be in days. - A object of class difftime - A character string, containing one of "day", "week", "month", "quarter" or "year". This can optionally be preceded by a (positive or negative) integer and a space, or followed by "s".

Value

an object of class Date

See also

ts_frequency() for standard aggregation. time_shift(), for shifting time stamps of a ts-boxable object.

Examples

ap.time <- ts_df(AirPassengers)$time head(date_month(ap.time))
#> [1] "1949-01-01" "1949-02-01" "1949-03-01" "1949-04-01" "1949-05-01" #> [6] "1949-06-01"
head(date_month(ap.time))
#> [1] "1949-01-01" "1949-02-01" "1949-03-01" "1949-04-01" "1949-05-01" #> [6] "1949-06-01"
head(date_year(ap.time))
#> [1] "1949-01-01" "1949-01-01" "1949-01-01" "1949-01-01" "1949-01-01" #> [6] "1949-01-01"
head(time_shift(ap.time, 14))
#> [1] "1949-01-15" "1949-02-15" "1949-03-15" "1949-04-15" "1949-05-15" #> [6] "1949-06-15"
head(time_shift(ap.time, "7 week"))
#> [1] "1949-02-19" "1949-03-22" "1949-04-19" "1949-05-20" "1949-06-19" #> [6] "1949-07-20"
head(time_shift(ap.time, "-1 month"))
#> [1] "1948-12-01" "1949-01-01" "1949-02-01" "1949-03-01" "1949-04-01" #> [6] "1949-05-01"