Vector Function API
Mapping functions that given a collection of items produce an array of values (a "vector") equal in length to the collection. Typically used with mutateWithSummary.
cumsum#
Returns a function that computes a cumulative sum as per d3-array::cumsum, using d3-array::fsum to reduce floating point errors.
Parameters#
key#
Either the key to compute the value over or an accessor function that maps a given item to the value to compute over.
Usage#
lag#
Lags a vector by a specified offset (options.n, default 1). Useful for finding previous values to compute deltas with later. It can be convenient to use TMath::subtract andTMath::add with these values to handle nulls.
Parameters#
key#
Either the key to compute the value over or an accessor function that maps a given item to the value to compute over.
options#
n = 1The number of positions to lag by. e.g. given[1,2,3,4]a lagnof 1 would produce[undefined,1,2,3]default = undefinedThe default value for non-existent rows (e.g. we've lagged before the first element).
Usage#
lead#
Leads a vector by a specified offset (options.n, default 1). Useful for finding next values to compute deltas with later. It can be convenient to use TMath::subtract andTMath::add with these values to handle nulls.
Parameters#
key#
Either the key to compute the value over or an accessor function that maps a given item to the value to compute over.
options#
n = 1The number of positions to lead by. e.g. given[1,2,3,4]a leadnof 1 would produce[2,3,4,undefined]default = undefinedThe default value for non-existent rows (e.g. we've lagged before the first element).
Usage#
roll#
Computes values over a rolling window. Typically used for calculating moving averages or running sums.
Parameters#
width#
The size of the window.
rollFn#
The function used to apply to the window, reduces to a single value for the window. Given the subset of items that are within the window as well as the ending index in the original array.
options#
partial = falseIf true, will compute the value even if the size of the window is less than the specified width. Otherwise, the rolled up value will be undefined.align = 'right'which direction the window is aligned to (default: right, looking back)- right: current row is the last item [ 1, 2, 3 ]
- left: current row is the first item [ 1, 2, 3 ]
- center: current row is the center item [ 1, 2, 3 ]
Usage#
rowNumber#
Computes the row number for each item in the array. Note this can also be accomplished with a simple mutate, if you prefer:
Parameters#
options#
startAt = 0what to start counting at, default is 0 โ so the first row is row 0, then row 1 for the second row.