Learn R Programming

xts (version 0.10-0)

timeBasedSeq: Create a Sequence or Range of Times

Description

A function to create a vector of time-based objects suitable for indexing an xts object, given a string conforming to the ISO 8601 time and date standard for range-based specification. The resultant series can be of any class supported by xts, including POSIXct, Date, chron, timeDate, yearmon, and yearqtr.

timeBasedRange creates a vector of length 1 or 2 as seconds since the epoch (1970-01-01) for use internally.

Usage

timeBasedSeq(x, retclass = NULL, length.out = NULL)

timeBasedRange(x, ...)

Arguments

x

a string representing the time-date range desired

retclass

the return class desired

length.out

passed to seq internally

unused

Value

A sequence or range of time-based objects.

If retclass is NULL, the result is a named list of from, to, by and length.out.

Details

Designed to provide uniform creation of valid time-based objects for use within xts, the interface conforms (mostly) to the ISO recommended format for specifying ranges.

In general, the format is a string specifying a time and/or date from, to, and optionally by delineated by either ‘"/"’ or ‘"::"’.

The first argument need not be quoted, as it is converted internally if need be.

The general form is from/to/by or from::to::by, where to and by are optional if the length.out arg is specified.

The from and to elements of the string must be left-specified with respect to the standard CCYYMMDD HHMMSS form. All dates-times specified will be set to either the earliest point (from) or the latest (to), given the level of specificity.

For example ‘1999’ in the from field would set the start to the beginning of 1999. The opposite occurs in the to field.

The level of detail in the request is interpretted as the level of detail in the result. The maximum detail of either from or to is the basis of the sequence, unless the optional by element is specified, which will be covered later.

To request a yearly series, it is only necessary to use ‘"1999/2008"’. Alternately, one could request a monthly series (returned by default as class yearmon) with ‘"199901/2008"’ or ‘"1999-01/2008"’, or even ‘"1999/2008-01"’. As the level of granularity increases, so does the resultant sequence granularity - as does its length.

Using the optional third by field (the third delimited element to the string), will override the granularity intepretation and return the requested periodicity. The acceptable arguments include Y for years, m for months, d for days, H for hours, M for minutes and S for seconds.

References

International Organization for Standardization: ISO 8601 https://www.iso.org

See Also

timeBased, xts

Examples

Run this code
# NOT RUN {
timeBasedSeq('1999/2008')
timeBasedSeq('199901/2008')
timeBasedSeq('199901/2008/d')
timeBasedSeq('20080101 0830',length=100) # 100 minutes
timeBasedSeq('20080101 083000',length=100) # 100 seconds
# }

Run the code above in your browser using DataLab