given this dataset here
Date Origin Destination Delay (mins)
03/01/2001 13:05:00 MCI MDW 8
03/01/2001 13:45:00 LAS PHX 95
03/01/2001 18:30:00 LAX PHX 10
03/01/2001 18:30:00 ONT PHX 0
03/01/2001 18:30:00 LAS LAX -9
03/01/2001 18:30:00 LAX OAK 12
03/01/2001 18:40:00 ONT SMF -8
03/01/2001 19:00:00 MDW BNA -1
03/01/2001 19:00:00 OAK LAX -10
03/01/2001 19:00:00 LAS LAX -11
...
How do I sum all the values in the Delay
field? I know there is minus values, I am just using this as an example.
I was thinking of using crossfilter to do this but not sure if there is a simplar way of doing this just writing a javascript function. for instance I can get the total records/rows with below which is 2692
cf.size()
//2692
NOTE
Bring up console mode (Ctrl-shift-I) and type cf.size()
the crossfilter is already defined in the source code(Ctrl-U) var cf = crossfilter(data);
EDIT1
using d3.sum
d3.sum(data,function(d){return d.delay;})
30632
using array.reduce - see JavaScript Array.prototype.reduce -what is the 0?
data.reduce(function(total,d){ return total+ d.delay }, 0)
30632
using crossfilter, but this is just using the reduce above on a crossfilter delay dimensionvar delayDimension = cf.dimension(function(d) {return d.delay;});
delayDimension.top(Infinity).reduce(function(total,d){ return total+ d.delay }, 0)
30632
NOTE:
This returns the delay value in the last element:
data.reduce(function(total,num){ return num.delay })
165
EDIT2
adapting on this: Custom calculated aggregated field in Crossfilter
create a dimension:
origin=cf.dimension(function(d) { return d.origin; })
create my reduceAdd, reduceMove, and reduceInitial functions:
var reduceAdd = function(p, v) {
p.delay += v.delay;
return p;
}
var reduceRemove = function(p, v) {
p.delay -= v.delay;
return p;
}
var reduceInitial = function() {
return {
delay : 0
}
}
Example output from console:
var json = origin.group().reduce(reduceAdd,reduceRemove,reduceInitial).orderNatural().top(Infinity);
undefined
json
[Objectkey: "ALB"value: Objectdelay: -1__proto__: Object__proto__: Object, Objectkey: "AMA"value: Objectdelay: 171__proto__: Object__proto__: Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
json[0]
Object {key: "ALB", value: Object}
json[0].value
Object {delay: -1}
Another good one here:
What are the reduceAdd, reduceSum , reduceRemove functions in crossfilter? How should they be used? but still is not exactly what I want, this is summing per category, I want a sum total e.g. expecting an answer of 30632
for all the delay values added together.
EDIT3
got it from the docs below printout from the console:
cf.size()
//2692
var reduceAdd = function(p, v) {
p.delay += v.delay;
return p;
}
var reduceRemove = function(p, v) {
p.delay -= v.delay;
return p;
}
var reduceInitial = function() {
return {
delay : 0
}
}
cf_groupAll=cf.groupAll().reduce(reduceAdd,reduceRemove,reduceInitial)
//Object {}
cf_groupAll.value()
//Object {delay: 30632}
cf_groupAll.value().delay
//30632
group.reduceSum
. The Crossfilter docs cover it here: github.com/crossfilter/crossfilter/wiki/… If you aren't using Crossfilter for interactivity, then I wouldn't use it at all. Just use Array.reduce (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…) or d3's d3.sum (github.com/d3/d3-array#sum) – Ethan Jewett