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.
#
cumsumReturns a function that computes a cumulative sum as per d3-array::cumsum, using d3-array::fsum to reduce floating point errors.
#
Parameterskey
#
Either the key to compute the value over or an accessor function that maps a given item to the value to compute over.
#
Usage#
lagLags 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.
#
Parameterskey
#
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 = 1
The number of positions to lag by. e.g. given[1,2,3,4]
a lagn
of 1 would produce[undefined,1,2,3]
default = undefined
The default value for non-existent rows (e.g. we've lagged before the first element).
#
Usage#
leadLeads 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.
#
Parameterskey
#
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 = 1
The number of positions to lead by. e.g. given[1,2,3,4]
a leadn
of 1 would produce[2,3,4,undefined]
default = undefined
The default value for non-existent rows (e.g. we've lagged before the first element).
#
Usage#
rollComputes values over a rolling window. Typically used for calculating moving averages or running sums.
#
Parameterswidth
#
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 = false
If 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#
rowNumberComputes the row number for each item in the array. Note this can also be accomplished with a simple mutate, if you prefer:
#
Parametersoptions
#
startAt = 0
what to start counting at, default is 0 โ so the first row is row 0, then row 1 for the second row.