ts_span: Limit Time Span

Description Usage Arguments Details Value Examples

View source: R/ts_span.R

Description

Filter time series for a time span.

Usage

1
2
3
4
5
ts_span(x, start = NULL, end = NULL, template = NULL)

ts_start(x)

ts_end(x)

Arguments

x

ts-boxable time series, an object of class ts, xts, zoo, data.frame, data.table, tbl, tbl_ts, tbl_time, or timeSeries.

start

start date, character string, Date or POSIXct

end

end date, character string, Date or POSIXct.

template

ts-boxable time series, an object of class ts, xts, data.frame, data.table, or tibble. If provided, from and to will be extracted from the object.

Details

All date and times, when entered as character strings, are processed by anytime::anydate() or anytime::anytime(). Thus a wide range of inputs are possible. See examples.

start and end can be specified relative to each other, using one of "sec", "min", "hour", "day", "week", "month", "quarter" or"year", or an abbreviation. If the series are of the same frequency, the shift can be specified in periods. See examples.

Value

a ts-boxable time series, with the same class as the input.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# use 'anytime' shortcuts
ts_span(mdeaths, start = "1979")       # shortcut for 1979-01-01
ts_span(mdeaths, start = "1979-4")     # shortcut for 1979-04-01
ts_span(mdeaths, start = "197904")     # shortcut for 1979-04-01

# it's fine to use an to date outside of series span
ts_span(mdeaths, end = "2001-01-01")

# use strings to set start or end relative to each other

ts_span(mdeaths, start = "-7 month")   # last 7 months
ts_span(mdeaths, start = -7)           # last 7 periods
ts_span(mdeaths, start = -1)           # last single value
ts_span(mdeaths, end = "1e4 hours")    # first 10000 hours


ts_plot(
  ts_span(mdeaths, start = "-3 years"),
  title = "Three years ago",
  subtitle = "The last three years of available data"
)

ts_ggplot(
  ts_span(mdeaths, end = "28 weeks"),
  title = "28 weeks later",
  subtitle = "The first 28 weeks of available data"
) + theme_tsbox() + scale_color_tsbox()


# Limit span of 'discoveries' to the same span as 'AirPassengers'
ts_span(discoveries, template = AirPassengers)

christophsax/tsbox documentation built on June 26, 2018, 1:13 p.m.