Grouping and aggregation

In the following example, we will perform grouping and aggregation in order to get statistics (Sum, Max, Min, and Average) about NBA players and which number of points they scored. First, the map function will send the name of the player and the number of points scored for each game. The map function will look like this:

function(){emit(this.player, this.points); }

Then, we can perform all the aggregation functions simultaneously using the sum method from the JavaScript Array object and the max/min functions of the JavaScript Math object. The reduce function will look like this:

function(key, values) { var explain = {total:Array.sum(values), max:Math.max.apply(Math, values), min:Math.min.apply(Math, values), avg:Array.sum(values)/values.length} ...

Get Practical Data Analysis - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.