77\alias {rollmean }
88\alias {frollmean }
99\alias {rollsum }
10- \alias {rollmax }
1110\alias {frollsum }
12- \alias {frollmax }
1311\alias {rollapply }
1412\alias {frollapply }
1513\title {Rolling functions }
@@ -21,9 +19,7 @@ frollmean(x, n, fill=NA, algo=c("fast", "exact"),
2119 align = c(" right" , " left" , " center" ), na.rm = FALSE , hasNA = NA , adaptive = FALSE )
2220frollsum(x , n , fill = NA , algo = c(" fast" ," exact" ),
2321 align = c(" right" , " left" , " center" ), na.rm = FALSE , hasNA = NA , adaptive = FALSE )
24- frollmax(x , n , fill = NA , algo = c(" fast" ," exact" ),
25- align = c(" right" , " left" , " center" ), na.rm = FALSE , hasNA = NA , adaptive = FALSE )
26- frollapply(x , n , FUN , \dots , fill = NA , align = c(" right" , " left" , " center" ), adaptive )
22+ frollapply(x , n , FUN , \dots , fill = NA , align = c(" right" , " left" , " center" ))
2723}
2824\arguments {
2925 \item {x }{ Vector , \code {data.frame } or \code {data.table } of integer , numeric or logical columns over which to calculate the windowed aggregations. May also be a list , in which case the rolling function is applied to each of its elements. }
@@ -49,69 +45,36 @@ frollapply(x, n, FUN, \dots, fill=NA, align=c("right", "left", "center"), adapti
4945 conveniently within \code {data.table } syntax.
5046
5147 Argument \code {n } allows multiple values to apply rolling functions on
52- multiple window sizes. If \code {adaptive = TRUE }, then \code {n } must be a list ,
53- see \emph {Adaptive rolling functions } section below for details.
48+ multiple window sizes. If \code {adaptive = TRUE }, then \code {n } must be a list.
49+ Each list element must be integer vector of window sizes corresponding
50+ to every single observation in each column ; see Examples.
5451
55- When multiple columns or multiple window widths are provided , then they
56- are run in parallel. The exception is for \code {algo = " exact" }, which runs in
57- parallel already. See \code {\link {setDTthreads }} for defaults and further details on parallelism in data.table.
58- }
59- \section {\code {hasNA } argument }{
60- \code {hasNA } can be used to speed up processing in cases when it is known
61- whether \code {x } contains infinite values \code {NA , NaN , + Inf , - Inf }.
62- \itemize {
63- \item { Default \code {hasNA = NA } will use faster \code {NA } agnostic implementation ,
64- but when \code {NA }s are detected it will re - run \code {NA } aware implementation. }
65- \item { \code {hasNA = TRUE } will use \code {NA } aware implementation straightaway. }
66- \item { \code {hasNA = FALSE } will use faster \code {NA } agnostic implementation.
67- Then depending on the rolling function it will either
68- \itemize {
69- \item { (\emph {mean , sum }) detect \code {NA }s , raise warning , re - run \code {NA } aware. }
70- \item { (\emph {max }) not detect \code {NA }s and may silently produce an incorrect
71- answer. }}
72- Therefore \code {hasNA = FALSE } should be used with care.
73- }
74- }
75- }
76- \section {Implementation }{
77- \itemize {
78- \item { \code {algo = " fast" } uses \emph {" on-line" } algorithm , and
79- all of \code {NaN , + Inf , - Inf } are treated as \code {NA }. Not all functions
80- have \emph {fast } implementation available. As of now
81- \emph {max } and \code {adaptive = TRUE } do not have it , therefore it will
82- automatically fall back to \emph {exact } implementation. \code {datatable.verbose }
83- option can be used to check that. }
84- \item { \code {algo = " exact" } will make rolling functions use a more
85- computationally - intensive algorithm. For each observation from input vector
86- it will compute a function on a window from scratch (complexity \eqn {O(n ^ 2 )}).
87- Depending on the function , this algorithm may suffers less from
88- floating point rounding error (the same consideration applies to base \code {\link [base ]{mean }}).
89- Algorithm also handles \code {NaN , + Inf , - Inf } consistently to
90- base R , unless - for some functions (e.g. \emph {max }) - \code {hasNA } is \code {FALSE }
91- but NAs are present. In case of some functions (like \emph {mean }), it will additionally
52+ When \code {algo = " fast" } an \emph {" on-line" } algorithm is used , and
53+ all of \code {NaN , + Inf , - Inf } are treated as \code {NA }.
54+ Setting \code {algo = " exact" } will make rolling functions to use a more
55+ computationally - intensive algorithm that suffers less from floating point
56+ rounding error (the same consideration applies to \code {\link [base ]{mean }}).
57+ \code {algo = " exact" } also handles \code {NaN , + Inf , - Inf } consistently to
58+ base R. In case of some functions (like \emph {mean }), it will additionally
9259 make extra pass to perform floating point error correction. Error
9360 corrections might not be truly exact on some platforms (like Windows )
94- when using multiple threads. }
95- }
96- }
97- \section {Adaptive rolling functions }{
61+ when using multiple threads.
62+
9863 Adaptive rolling functions are a special case where each
99- observation has its own corresponding rolling window width. \code {n }
100- argument must be a list , then each list element must be an integer vector
101- of window sizes corresponding to every single observation in each column ;
102- see Examples. Due to the logic or implementation of adaptive rolling
103- functions , the following restrictions apply :
64+ observation has its own corresponding rolling window width. Due to the logic
65+ of adaptive rolling functions , the following restrictions apply :
10466 \itemize {
105- \item \code {align } does not support \code {" center " }.
67+ \item \code {align } only \code {" right " }.
10668 \item if list of vectors is passed to \code {x }, then all
10769 vectors within it must have equal length.
108- \item functionality is not suported in \code {frollapply }.
10970 }
110- }
111- \section {\code {frollapply }}{
71+
72+ When multiple columns or multiple windows width are provided , then they
73+ are run in parallel. The exception is for \code {algo = " exact" }, which runs in
74+ parallel already.
75+
11276 \code {frollapply } computes rolling aggregate on arbitrary R functions.
113- \code {adaptive } argument is not supported. The input
114- \code {x } (first argument ) to the function \code {FUN }
77+ The input \code {x } (first argument ) to the function \code {FUN }
11578 is coerced to \emph {numeric } beforehand and \code {FUN }
11679 has to return a scalar \emph {numeric } value. Checks for that are made only
11780 during the first iteration when \code {FUN } is evaluated. Edge cases can be
@@ -121,34 +84,32 @@ frollapply(x, n, FUN, \dots, fill=NA, align=c("right", "left", "center"), adapti
12184 because there is no thread - safe API to R ' s C \c ode{eval}. Nevertheless we' ve
12285 seen the computation speed up vis - a - vis versions implemented in base R.
12386}
124- \section {\code {zoo } package users notice }{
87+ \value {
88+ A list except when the input is a \code {vector } and
89+ \code {length(n )== 1 } in which case a \code {vector } is returned.
90+ }
91+ \note {
12592 Users coming from most popular package for rolling functions
12693 \code {zoo } might expect following differences in \code {data.table }
12794 implementation.
12895 \itemize {
129- \item rolling functions will always return result of the same length
130- as input.
131- \item \code {fill } defaults to \code {NA }.
96+ \item rolling function will always return result of the same length as input.
97+ \item \code {fill } defaults to \code {NA }.
13298 \item \code {fill } accepts only constant values. It does not support
13399 for \emph {na.locf } or other functions.
134- \item \code {align } defaults to \code {" right" }.
100+ \item \code {align } defaults to \code {" right" }.
135101 \item \code {na.rm } is respected , and other functions are not needed
136102 when input contains \code {NA }.
137- \item integers and logical are always coerced to double.
103+ \item integers and logical are always coerced to double.
138104 \item when \code {adaptive = FALSE } (default ), then \code {n } must be a
139105 numeric vector. List is not accepted.
140106 \item when \code {adaptive = TRUE }, then \code {n } must be vector of
141107 length equal to \code {nrow(x )}, or list of such vectors.
142108 \item \code {partial } window feature is not supported , although it can
143- be accomplished by using \code {adaptive = TRUE }, see examples.
144- \code {NA } is always returned for incomplete windows.
109+ be accomplished by using \code {adaptive = TRUE }, see
110+ examples. \code {NA } is always returned for incomplete windows.
145111 }
146- }
147- \value {
148- A list except when the input is a \code {vector } and
149- \code {length(n )== 1 } in which case a \code {vector } is returned.
150- }
151- \note {
112+
152113 Be aware that rolling functions operates on the physical order of input.
153114 If the intent is to roll values in a vector by a logical window , for
154115 example an hour , or a day , one has to ensure that there are no gaps in
0 commit comments