-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reusable custom reduce functions #102
Comments
What about: function reduceAddAvg(attr) {
return function(p,v) {
++p.count
p.sum += v[attr];
p.avg = p.sum/p.count;
return p;
};
}
function reduceRemoveAvg(attr) {
return function(p,v) {
--p.count
p.sum -= v[attr];
p.avg = p.sum/p.count;
return p;
};
}
function reduceInitAvg() {
return {count:0, sum:0, avg:0};
}
var statesAvgGroup = statesAvgDimension.group().reduce(reduceAddAvg('savings'), reduceRemoveAvg('savings'), reduceInitAvg);
var statesAvgGroup = statesAvgDimension.group().reduce(reduceAddAvg('cost'), reduceRemoveAvg('cost'), reduceInitAvg);
// ... and so on Would that work? |
Yep, this works great. Maybe a good one for the docs? It's obvious when I see it but wasn't obvious before! |
Great! I added a note linking here from https://github.com/square/crossfilter/wiki/API-Reference#wiki-group_reduce |
nice |
Why isn't statesAvgGroup overwritten by the subsequent declarations? |
Note that you need to guard against a divide by zero in p.avg = p.count ? p.sum/p.count : 0; |
Derived calculations, such as The final reduction can be done at the end of an interaction, i.e. after applying a filter or adding an array of new elements. The aggregate is just a tuple of It conveniently makes the user responsible for handling the degenerate case of the empty set (e.g. they can check With some aggregates, non-empty sets of reasonable values can still lead to edge cases. E.g. computing the extent where all N numbers are of the same value. The user of the values can determine what to do with the edge case, e.g. just not rendering axis ticks, or adding 1 to As a more complicated, yet still incrementally computable aggregate, consider the standard deviation or its cousin, variance. The final reduction is division and taking the square root. A more challenging group aggregate, surprisingly, is the calculation of the extent (or just a minimum or maximum). The difficulty is updating the aggregate with items removed. A basic option is to maintain a sorted value vector but it gets expensive. |
* Fix type definition for dimension.group() * Fix casing of word 'TKey'
I want to calculate the average of a lot of attributes, such as cost, savings, carbon emmissions, and so on.
Here is an example of calculating it with a single attribute (savings):
But I am going to have to write a set of these reduce functions for every attribute I want to calculate (up to a dozen attributes). I am hoping there is a way to make these functions reusable. For example, something like this:
Would this be a feature request, impossible, or am I missing something in the docs?
Thanks!
The text was updated successfully, but these errors were encountered: