Title: | 'data.table' Time-Series |
---|---|
Description: | High-frequency time-series support via 'nanotime' and 'data.table'. |
Authors: | Dirk Eddelbuettel [aut, cre] |
Maintainer: | Dirk Eddelbuettel <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.1.3 |
Built: | 2024-08-24 00:46:09 UTC |
Source: | https://github.com/eddelbuettel/dtts |
data.table
onto a nanotime
vectoralign
returns the subset of data.table
x
that
aligns on the temporal vector y
align(x, y, start, end, ...) ## S4 method for signature 'data.table,nanotime,nanoduration,nanoduration' align( x, y, start = as.nanoduration(0), end = as.nanoduration(0), sopen = FALSE, eopen = TRUE, func = NULL ) ## S4 method for signature 'data.table,nanotime,missing,missing' align( x, y, start = as.nanoduration(0), end = as.nanoduration(0), sopen = FALSE, eopen = TRUE, func = NULL ) ## S4 method for signature 'data.table,nanotime,nanoduration,missing' align( x, y, start = as.nanoduration(0), end = as.nanoduration(0), sopen = FALSE, eopen = TRUE, func = NULL ) ## S4 method for signature 'data.table,nanotime,missing,nanoduration' align( x, y, start = as.nanoduration(0), end = as.nanoduration(0), sopen = FALSE, eopen = TRUE, func = NULL ) ## S4 method for signature 'data.table,nanotime,nanoperiod,nanoperiod' align( x, y, start = as.nanoperiod(0), end = as.nanoperiod(0), sopen = FALSE, eopen = TRUE, tz, func = NULL ) ## S4 method for signature 'data.table,nanotime,nanoperiod,missing' align( x, y, start = as.nanoperiod(0), end = as.nanoperiod(0), sopen = FALSE, eopen = TRUE, tz, func = NULL ) ## S4 method for signature 'data.table,nanotime,missing,nanoperiod' align( x, y, start = as.nanoperiod(0), end = as.nanoperiod(0), sopen = FALSE, eopen = TRUE, tz, func = NULL )
align(x, y, start, end, ...) ## S4 method for signature 'data.table,nanotime,nanoduration,nanoduration' align( x, y, start = as.nanoduration(0), end = as.nanoduration(0), sopen = FALSE, eopen = TRUE, func = NULL ) ## S4 method for signature 'data.table,nanotime,missing,missing' align( x, y, start = as.nanoduration(0), end = as.nanoduration(0), sopen = FALSE, eopen = TRUE, func = NULL ) ## S4 method for signature 'data.table,nanotime,nanoduration,missing' align( x, y, start = as.nanoduration(0), end = as.nanoduration(0), sopen = FALSE, eopen = TRUE, func = NULL ) ## S4 method for signature 'data.table,nanotime,missing,nanoduration' align( x, y, start = as.nanoduration(0), end = as.nanoduration(0), sopen = FALSE, eopen = TRUE, func = NULL ) ## S4 method for signature 'data.table,nanotime,nanoperiod,nanoperiod' align( x, y, start = as.nanoperiod(0), end = as.nanoperiod(0), sopen = FALSE, eopen = TRUE, tz, func = NULL ) ## S4 method for signature 'data.table,nanotime,nanoperiod,missing' align( x, y, start = as.nanoperiod(0), end = as.nanoperiod(0), sopen = FALSE, eopen = TRUE, tz, func = NULL ) ## S4 method for signature 'data.table,nanotime,missing,nanoperiod' align( x, y, start = as.nanoperiod(0), end = as.nanoperiod(0), sopen = FALSE, eopen = TRUE, tz, func = NULL )
x |
the |
y |
the |
start |
scalar or vector of same length as |
end |
scalar or vector of same length as |
... |
further arguments passed to or from methods. |
sopen |
boolean scalar or vector of same lengths as |
eopen |
boolean scalar or vector of same lengths as |
func |
a function taking one argument and which provides an
arbitrary aggregation of its argument; if |
tz |
scalar or vector of same length as |
For each element in y
, intervals are created around this
element with start
and end
. All the elements of
x
that fall within this interval are given as argument to
the function func
. The function func
show reduce
this data.frame
to one unique row that will be associated
with the nanotime
value in y
.
a data.table
time-series of the same length as
y
; this is a subset of x
with the
nanotime
index of y
## Not run: y <- nanotime((1:10)*1e9) x <- data.table(index=nanotime((1:10)*1e9), data=1:10) align(x, y, as.nanoduration(-1e9), as.nanoduration(1e9), colMeans) ## End(Not run)
## Not run: y <- nanotime((1:10)*1e9) x <- data.table(index=nanotime((1:10)*1e9), data=1:10) align(x, y, as.nanoduration(-1e9), as.nanoduration(1e9), colMeans) ## End(Not run)
align_idx
returns the index of the alignment of x
on y
align_idx(x, y, start, end, ...) ## S4 method for signature 'nanotime,nanotime,nanoduration,nanoduration' align_idx( x, y, start, end, sopen = FALSE, eopen = TRUE, bypass_x_check = FALSE, bypass_y_check = FALSE ) ## S4 method for signature 'nanotime,nanotime,missing,missing' align_idx( x, y, start, end, sopen = FALSE, eopen = TRUE, bypass_x_check = FALSE, bypass_y_check = FALSE ) ## S4 method for signature 'nanotime,nanotime,missing,nanoduration' align_idx( x, y, start, end, sopen = FALSE, eopen = TRUE, bypass_x_check = FALSE, bypass_y_check = FALSE ) ## S4 method for signature 'nanotime,nanotime,nanoduration,missing' align_idx( x, y, start, end, sopen = FALSE, eopen = TRUE, bypass_x_check = FALSE, bypass_y_check = FALSE ) ## S4 method for signature 'nanotime,nanotime,nanoperiod,nanoperiod' align_idx( x, y, start = as.nanoperiod(0), end = as.nanoperiod(0), sopen = FALSE, eopen = TRUE, tz, bypass_x_check = FALSE, bypass_y_check = FALSE ) ## S4 method for signature 'nanotime,nanotime,missing,nanoperiod' align_idx( x, y, start = as.nanoperiod(0), end = as.nanoperiod(0), sopen = FALSE, eopen = TRUE, tz, bypass_x_check = FALSE, bypass_y_check = FALSE ) ## S4 method for signature 'nanotime,nanotime,nanoperiod,missing' align_idx( x, y, start = as.nanoperiod(0), end = as.nanoperiod(0), sopen = FALSE, eopen = TRUE, tz, bypass_x_check = FALSE, bypass_y_check = FALSE )
align_idx(x, y, start, end, ...) ## S4 method for signature 'nanotime,nanotime,nanoduration,nanoduration' align_idx( x, y, start, end, sopen = FALSE, eopen = TRUE, bypass_x_check = FALSE, bypass_y_check = FALSE ) ## S4 method for signature 'nanotime,nanotime,missing,missing' align_idx( x, y, start, end, sopen = FALSE, eopen = TRUE, bypass_x_check = FALSE, bypass_y_check = FALSE ) ## S4 method for signature 'nanotime,nanotime,missing,nanoduration' align_idx( x, y, start, end, sopen = FALSE, eopen = TRUE, bypass_x_check = FALSE, bypass_y_check = FALSE ) ## S4 method for signature 'nanotime,nanotime,nanoduration,missing' align_idx( x, y, start, end, sopen = FALSE, eopen = TRUE, bypass_x_check = FALSE, bypass_y_check = FALSE ) ## S4 method for signature 'nanotime,nanotime,nanoperiod,nanoperiod' align_idx( x, y, start = as.nanoperiod(0), end = as.nanoperiod(0), sopen = FALSE, eopen = TRUE, tz, bypass_x_check = FALSE, bypass_y_check = FALSE ) ## S4 method for signature 'nanotime,nanotime,missing,nanoperiod' align_idx( x, y, start = as.nanoperiod(0), end = as.nanoperiod(0), sopen = FALSE, eopen = TRUE, tz, bypass_x_check = FALSE, bypass_y_check = FALSE ) ## S4 method for signature 'nanotime,nanotime,nanoperiod,missing' align_idx( x, y, start = as.nanoperiod(0), end = as.nanoperiod(0), sopen = FALSE, eopen = TRUE, tz, bypass_x_check = FALSE, bypass_y_check = FALSE )
x |
the |
y |
the |
start |
scalar or vector of same length as |
end |
scalar or vector of same length as |
... |
further arguments passed to or from methods. |
sopen |
boolean scalar or vector of same lengths as |
eopen |
boolean scalar or vector of same lengths as |
bypass_x_check |
logical indicating if the sorting of
|
bypass_y_check |
logical indicating if the sorting of
|
tz |
scalar or vector of same length as |
In order to perform the alignment, intervals are created around
each elements in y
using start
and end
. For
each such interval, the closest element in x
is chosen. If
no element in x
falls in the interval, then NaN is
returned.
When only x
and y
are specified, the
default is to close the intervals so that the alignment simply
picks up equal points. Note that it is possible to specify
meaningless intervals, for instance with a start
that
is beyond end
. In this case, the alignment will simply
return NA for each element in y
. In principle, the
start
and end
are chosen to define an interval
is the past, or around the points in y
, but if they are
both positive, they can define intervals in the future.
a vector of indices of the same length as y
; this
vector indexes into x
and represent the closest point
of x
that is in the interval defined around each point
in y
## Not run: align_idx(nanotime(c(10:14, 17:19)), nanotime(11:20)) ## [1] 2 3 4 5 NA NA 6 7 8 NA ## End(Not run)
## Not run: align_idx(nanotime(c(10:14, 17:19)), nanotime(11:20)) ## [1] 2 3 4 5 NA NA 6 7 8 NA ## End(Not run)
frequency
returns the number of observations in
data.table
x
for each interval specified by
by
.
## S4 method for signature 'data.table' frequency( x, by, grid_start, grid_end, tz, ival_start = -by, ival_end, ival_sopen = FALSE, ival_eopen = TRUE )
## S4 method for signature 'data.table' frequency( x, by, grid_start, grid_end, tz, ival_start = -by, ival_end, ival_sopen = FALSE, ival_eopen = TRUE )
x |
the |
by |
interval specified as a |
grid_start |
scalar |
grid_end |
scalar |
tz |
scalar of type character. Only used when the type of
|
ival_start |
scalar of type |
ival_end |
scalar of type |
ival_sopen |
boolean scalar that indicates if the start of the interval is open or closed. Defaults to FALSE. |
ival_eopen |
boolean scalar that indicates if the end of the interval is open or closed. Defaults to TRUE. |
a data.table
time-series with the number of
observations in x
that fall withing the intervals
defined by the grid interval defined by by
.
## Not run: one_second <- as.nanoduration("00:00:01") one_minute <- 60 * one_second x <- data.table(index=nanotime((1:100) * one_second), 1) setkey(x, index) frequency(x, one_minute) ## End(Not run)
## Not run: one_second <- as.nanoduration("00:00:01") one_minute <- 60 * one_second x <- data.table(index=nanotime((1:100) * one_second), 1) setkey(x, index) frequency(x, one_minute) ## End(Not run)
data.table
onto a nanotime
vector gridgrid_align
returns the subset of data.table
x
that aligns on the grid defined by by
, start
and
end
grid_align(x, by, ...) ## S4 method for signature 'data.table,nanoduration' grid_align( x, by, func = NULL, grid_start = x[[1]][1] + by, grid_end = tail(x[[1]], 1), ival_start = -by, ival_end = as.nanoduration(0), ival_sopen = FALSE, ival_eopen = TRUE ) ## S4 method for signature 'data.table,nanoperiod' grid_align( x, by, func = NULL, grid_start = plus(x[[1]][1], by, tz), grid_end = tail(x[[1]], 1), ival_start = -by, ival_end = as.nanoperiod(0), ival_sopen = FALSE, ival_eopen = TRUE, tz )
grid_align(x, by, ...) ## S4 method for signature 'data.table,nanoduration' grid_align( x, by, func = NULL, grid_start = x[[1]][1] + by, grid_end = tail(x[[1]], 1), ival_start = -by, ival_end = as.nanoduration(0), ival_sopen = FALSE, ival_eopen = TRUE ) ## S4 method for signature 'data.table,nanoperiod' grid_align( x, by, func = NULL, grid_start = plus(x[[1]][1], by, tz), grid_end = tail(x[[1]], 1), ival_start = -by, ival_end = as.nanoperiod(0), ival_sopen = FALSE, ival_eopen = TRUE, tz )
x |
the |
by |
interval specified as a |
... |
further arguments passed to or from methods. |
func |
a function taking one argument and which provides an
arbitrary aggregation of its argument; if |
grid_start |
scalar |
grid_end |
scalar |
ival_start |
scalar of type |
ival_end |
scalar of type |
ival_sopen |
boolean scalar that indicates if the start of the interval is open or closed. Defaults to FALSE. |
ival_eopen |
boolean scalar that indicates if the end of the interval is open or closed. Defaults to TRUE. |
tz |
scalar of type character. Only used when the type of
|
A grid defined by the parameter by
, start
and
end
is created. The function then does a standard alignment
of x
onto this grid (see the align
function)
a data.table
time-series of the same length as
y
with the aggregations computed by func
## Not run: one_second <- 1e9 x <- data.table(index=nanotime(cumsum(sin(seq(0.001, pi, 0.001)) * one_second))) x <- x[, V2 := 1:nrow(x)] setkey(x, index) grid_align(x, as.nanoduration("00:01:00"), sum) ## End(Not run)
## Not run: one_second <- 1e9 x <- data.table(index=nanotime(cumsum(sin(seq(0.001, pi, 0.001)) * one_second))) x <- x[, V2 := 1:nrow(x)] setkey(x, index) grid_align(x, as.nanoduration("00:01:00"), sum) ## End(Not run)
data.table
time-seriesops
returns the y
time-series on which the x
time-series values are applied using the specified operator
op
.
ops(x, y, op_string) ## S4 method for signature 'data.table,data.table,character' ops(x, y, op_string)
ops(x, y, op_string) ## S4 method for signature 'data.table,data.table,character' ops(x, y, op_string)
x |
the |
y |
the |
op_string |
string defining the operation to apply; the
supported values for |
The n elements of the x
time-series operand define a set of
n-1 intervals, and the value associated with each interval is
applied to all the observations in the y
time-series
operand that fall in the interval. Note that the interval is
closed at the beginning and open at the end. The supported values
for op
are "*", "/", "+", "-".
There has to be one numeric column in x
and y
; there
has to be either a one to one correspondance between the number of
numeric columns in x
and y
, or there must be only
one numeric column in x
that will be applied to all numeric
columns in y
. Non-numeric columns must not appear in
x
, whereas they will be skipped of they appear in y
.
## Not run: one_second_duration <- as.nanoduration("00:00:01") t1 <- nanotime(1:2 * one_second_duration * 3) t2 <- nanotime(1:4 * one_second_duration) dt1 <- data.table(index=t1, data1 = 1:length(t1)) setkey(dt1, index) dt2 <- data.table(index=t2, data1 = 1:length(t2)) setkey(dt2, index) ops(dt1, dt2, "+") ## End(Not run)
## Not run: one_second_duration <- as.nanoduration("00:00:01") t1 <- nanotime(1:2 * one_second_duration * 3) t2 <- nanotime(1:4 * one_second_duration) dt1 <- data.table(index=t1, data1 = 1:length(t1)) setkey(dt1, index) dt2 <- data.table(index=t2, data1 = 1:length(t2)) setkey(dt2, index) ops(dt1, dt2, "+") ## End(Not run)