Skip to contents

Periodic methods for base R functions.

Usage

# S3 method for PeriodicTS
window(x, start = NULL, end = NULL, seasons = NULL, ...)

# S3 method for PeriodicMTS
window(x, start = NULL, end = NULL, seasons = NULL, ...)

# S3 method for PeriodicTS
na.trim(object, sides = c("both", "left", "right"), ...)

# S3 method for PeriodicMTS
na.trim(object, sides = c("both", "left", "right"), 
        is.na = c("any", "all"), ...)

# S3 method for PeriodicTimeSeries
frequency(x, ...)

# S3 method for PeriodicTimeSeries
deltat(x, ...)

# S3 method for PeriodicTimeSeries
cycle(x, ...)

# S3 method for PeriodicTimeSeries
time(x, offset = 0, ...)

# S3 method for Cyclic
start(x, ...)

# S3 method for Cyclic
end(x, ...)

Arguments

x,object

an object from the indicated periodic class.

start

numeric(2), start time.

end

numeric(2), end time.

seasons

numeric, a subset of 1:nSeasons(x).

...

Not used by these methods.

sides

which side to trim: start ("left"), end ("right"), or both ("both").

is.na

for multivariate time series: if "all", the observation at time \(t\) will be considered missing only if all variables are NA at that time. Otherwise, if "any", any variable with value NA will cause the observation at time \(t\) to be considered missing.

offset

currently ignored (:TODO:)

Details

Periodic methods for base R and other common functions for manipulation of time series. These methods work analogoulsly to their base R cousins and only the differences, if any, are discussed below.

window takes a part of x, preserving the class of the object. Argument seasons selects a subset of the seasons.

na.trim is a function defined in package zoo and re-exported by pcts. It trims NAs from one or both ends of the time series, as requested by the arguments. The arguments of the methods defined by pcts have the same meaning as those in zoo.

Value

for window and na.trim, an object from the same class as the original, representing the requested part of the time series. for frequency, an integer number. for deltat, a number (1/frequency). for cycle and time, a "PeriodicTS" object. for start and end, time of first/last observation, encoded as a pair of numbers.

See also

window, frequency, na.trim for details on what these functions do.

availStart and availEnd give the times of the first and last non-NA observations.

Examples

pres <- pcts(presidents)
head(pres, 8)
#> An object of class "PeriodicTS"
#> Slot "cycle": Start:  1945   Quarter_1 
#> Cycle:  QuarterYearCycle 
#> Number of seasons: 4 
#> 
#>       Q1 Q2 Q3 Q4
#> Y1945 NA 87 82 75
#> Y1946 63 50 43 32
availStart(pres)
#> [1] 1945    2

tail(pres, 12)
#> An object of class "PeriodicTS"
#> Slot "cycle": Start:  1972   Quarter_1 
#> Cycle:  QuarterYearCycle 
#> Number of seasons: 4 
#> 
#>       Q1 Q2 Q3 Q4
#> Y1972 49 61 NA NA
#> Y1973 68 44 40 27
#> Y1974 28 25 24 24
availEnd(pres)
#> [1] 1974    4

## Q3 and Q4 only
presQ3Q4 <- window(pres, seasons = 3:4)
head(presQ3Q4)
#> An object of class "PeriodicTS"
#> Slot "cycle": Start:  1945   Quarter_3 
#> Cycle:  PartialCycle 
#> Number of seasons: 2 
#> 
#>       Q3 Q4
#> Y1945 82 75
#> Y1946 43 32
#> Y1947 54 55

identical(na.trim(pres),
          window(pres, start = availStart(pres), end = availEnd(pres)))
#> [1] TRUE
## TRUE