Convert an OHLC or univariate object to a specified periodicity lower than the given data object. For example, convert a daily series to a monthly series, or a monthly series to a yearly one, or a one minute series to an hourly series.
to.period(
x,
period = "months",
k = 1,
indexAt = NULL,
name = NULL,
OHLC = TRUE,
...
)
to.minutes(x, k, name, ...)
to.minutes3(x, name, ...)
to.minutes5(x, name, ...)
to.minutes10(x, name, ...)
to.minutes15(x, name, ...)
to.minutes30(x, name, ...)
to.hourly(x, name, ...)
to.daily(x, drop.time = TRUE, name, ...)
to.weekly(x, drop.time = TRUE, name, ...)
to.monthly(x, indexAt = "yearmon", drop.time = TRUE, name, ...)
to.quarterly(x, indexAt = "yearqtr", drop.time = TRUE, name, ...)
to.yearly(x, drop.time = TRUE, name, ...)A univariate or OHLC type time-series object.
Period to convert to. See details.
Number of sub periods to aggregate on (only for minutes and seconds).
Convert final index to new class or date. See details.
Override column names?
Should an OHLC object be returned? (only OHLC = TRUE
currently supported)
Additional arguments.
Remove time component of POSIX datestamp (if any)?
An object of the original type, with new periodicity.
The result will contain the open and close for the given period, as well as the maximum and minimum over the new period, reflected in the new high and low, respectively. Aggregate volume will also be calculated if applicable.
An easy and reliable way to convert one periodicity of data into any new
periodicity. It is important to note that all dates will be aligned to the
end of each period by default - with the exception of to.monthly() and
to.quarterly(), which use the zoo package's yearmon and
yearqtr classes, respectively.
Valid period character strings include: "seconds", "minutes", "hours",
"days", "weeks", "months", "quarters", and "years". These are
calculated internally via endpoints(). See that function's help page for
further details.
To adjust the final indexing style, it is possible to set indexAt to one
of the following: ‘yearmon’, ‘yearqtr’, ‘firstof’,
‘lastof’, ‘startof’, or ‘endof’. The final index will
then be yearmon, yearqtr, the first time of the period, the last time
of the period, the starting time in the data for that period, or the ending
time in the data for that period, respectively.
It is also possible to pass a single time series, such as a univariate exchange rate, and return an OHLC object of lower frequency - e.g. the weekly OHLC of the daily series.
Setting drop.time = TRUE (the default) will convert a series that includes
a time component into one with just a date index, since the time component
is often of little value in lower frequency series.
In order for this function to work properly on OHLC data, it is
necessary that the Open, High, Low and Close columns be names as such;
including the first letter capitalized and the full spelling found.
Internally a call is made to reorder the data into the correct column order,
and then a verification step to make sure that this ordering and naming has
succeeded. All other data formats must be aggregated with functions such as
aggregate() and period.apply().
This method should work on almost all time-series-like objects. Including ‘timeSeries’, ‘zoo’, ‘ts’, and ‘irts’. It is even likely to work well for other data structures - including ‘data.frames’ and ‘matrix’ objects.
Internally a call to as.xts() converts the original x into the
universal xts format, and then re-converts back to the original type.
A special note with respect to ‘ts’ objects. As these are strictly
regular they may include NA values. These are removed before aggregation,
though replaced before returning the result. This inevitably leads to many
additional NA values in the result. Consider using an xts object or
converting to xts using as.xts().
data(sample_matrix)
samplexts <- as.xts(sample_matrix)
to.monthly(samplexts)
#> samplexts.Open samplexts.High samplexts.Low samplexts.Close
#> Jan 2007 50.03978 50.77336 49.76308 50.22578
#> Feb 2007 50.22448 51.32342 50.19101 50.77091
#> Mar 2007 50.81620 50.81620 48.23648 48.97490
#> Apr 2007 48.94407 50.33781 48.80962 49.33974
#> May 2007 49.34572 49.69097 47.51796 47.73780
#> Jun 2007 47.74432 47.94127 47.09144 47.76719
to.monthly(sample_matrix)
#> sample_matrix.Open sample_matrix.High sample_matrix.Low
#> Jan 2007 50.03978 50.77336 49.76308
#> Feb 2007 50.22448 51.32342 50.19101
#> Mar 2007 50.81620 50.81620 48.23648
#> Apr 2007 48.94407 50.33781 48.80962
#> May 2007 49.34572 49.69097 47.51796
#> Jun 2007 47.74432 47.94127 47.09144
#> sample_matrix.Close
#> Jan 2007 50.22578
#> Feb 2007 50.77091
#> Mar 2007 48.97490
#> Apr 2007 49.33974
#> May 2007 47.73780
#> Jun 2007 47.76719
str(to.monthly(samplexts))
#> An xts object on Jan 2007 / Jun 2007 containing:
#> Data: double [6, 4]
#> Columns: samplexts.Open, samplexts.High, samplexts.Low, samplexts.Close
#> Index: yearmon [6] (TZ: "UTC")
str(to.monthly(sample_matrix))
#> num [1:6, 1:4] 50 50.2 50.8 48.9 49.3 ...
#> - attr(*, "dimnames")=List of 2
#> ..$ : chr [1:6] "Jan 2007" "Feb 2007" "Mar 2007" "Apr 2007" ...
#> ..$ : chr [1:4] "sample_matrix.Open" "sample_matrix.High" "sample_matrix.Low" "sample_matrix.Close"