Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

subset.ts

Subsetting a time series


Description

Various types of subsetting of a time series. Allows subsetting by index values (unlike window). Also allows extraction of the values of a specific season or subset of seasons in each year. For example, to extract all values for the month of May from a time series.

Usage

## S3 method for class 'ts'
subset(
  x,
  subset = NULL,
  month = NULL,
  quarter = NULL,
  season = NULL,
  start = NULL,
  end = NULL,
  ...
)

## S3 method for class 'msts'
subset(x, subset = NULL, start = NULL, end = NULL, ...)

Arguments

x

a univariate time series to be subsetted

subset

optional logical expression indicating elements to keep; missing values are taken as false. subset must be the same length as x.

month

Numeric or character vector of months to retain. Partial matching on month names used.

quarter

Numeric or character vector of quarters to retain.

season

Numeric vector of seasons to retain.

start

Index of start of contiguous subset.

end

Index of end of contiguous subset.

...

Other arguments, unused.

Details

If character values for months are used, either upper or lower case may be used, and partial unambiguous names are acceptable. Possible character values for quarters are "Q1", "Q2", "Q3", and "Q4".

Value

If subset is used, a numeric vector is returned with no ts attributes. If start and/or end are used, a ts object is returned consisting of x[start:end], with the appropriate time series attributes retained. Otherwise, a ts object is returned with frequency equal to the length of month, quarter or season.

Author(s)

Rob J Hyndman

See Also

Examples

plot(subset(gas,month="November"))
subset(woolyrnq,quarter=3)
subset(USAccDeaths, start=49)

forecast

Forecasting Functions for Time Series and Linear Models

v8.14
GPL-3
Authors
Rob Hyndman [aut, cre, cph] (<https://orcid.org/0000-0002-2140-5352>), George Athanasopoulos [aut], Christoph Bergmeir [aut] (<https://orcid.org/0000-0002-3665-9021>), Gabriel Caceres [aut], Leanne Chhay [aut], Mitchell O'Hara-Wild [aut] (<https://orcid.org/0000-0001-6729-7695>), Fotios Petropoulos [aut] (<https://orcid.org/0000-0003-3039-4955>), Slava Razbash [aut], Earo Wang [aut], Farah Yasmeen [aut] (<https://orcid.org/0000-0002-1479-5401>), R Core Team [ctb, cph], Ross Ihaka [ctb, cph], Daniel Reid [ctb], David Shaub [ctb], Yuan Tang [ctb] (<https://orcid.org/0000-0001-5243-233X>), Zhenyu Zhou [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.