Subsetting a window of observations

Manipulating Time Series Data in R

Harrison Brown

Graduate Researcher in Geography

Time series windows

Windows:

  • Subset of time series
  • Inherits frequency from parent time series
  • Defined by start and end point

Purpose of windows:

  • View a specified range of data
  • Focus in on years/events of interest
  • Ignore observations at the "edge" of the data
Manipulating Time Series Data in R

Time series windows

card_prices
2013-01-01 4.88
2013-01-02 5.03
2013-01-03 5.11
...
2014-12-29 4.98
2014-12-30 5.01
2014-12-31 4.94
card_window <- stats::window(
  x = card_prices,
  start = "2013-05-01",
  end = "2013-07-01")
card_window
2013-05-01 4.98
2013-05-02 4.92
...
2013-07-30 5.04
2013-07-31 4.96
  • start and end are inclusive
Manipulating Time Series Data in R

Subsetting by logical expressions

subset <-
  index(card_price) >= "2013-05-01" &
  index(card_price) <= "2013-07-31"

subset
[1] FALSE FALSE FALSE
[4] FALSE FALSE FALSE
...
card_subset <- card_price[subset]
card_subset
2013-05-01 4.98
2013-05-02 4.92
2013-05-03 5.21
...
2013-07-29 4.89
2013-07-30 5.04
2013-07-31 4.96
Manipulating Time Series Data in R

Extracting specific dates

card_price["2014-02-28"]
2014-02-28 
      5.02
class(index(card_price))
[1] "Date"
class("2014-02-28")
[1] "character"
Manipulating Time Series Data in R

Let's practice!

Manipulating Time Series Data in R

Preparing Video For Download...