D3.js: how to find Average grouped by Year and Month?
<p>I am trying to find the monthly average of all the values(i.e. duration here) linked to all the activities(i.e. fetched in keys) with respect to their year.</p>

<p>My csv is like:</p>

<pre><code>ID      TEST_DATE     TEST_DURATION    TEST_ACTIVITY
10000   1/1/2014 0:00   4              Electricity Bill Payment
10001   1/2/2014 0:00   1              Water Bill Payment
10002   1/3/2014 0:00   2              Gas Bill Payment
10003   1/4/2014 0:00   1              Electricity Bill Payment
10001   1/2/2014 0:00   1              Water Bill Payment
10001   1/2/2014 0:00   1              Water Bill Payment
14878   9/12/2016       6              Statement Request
</code></pre>

<p>My code is as follows:</p>

<pre><code>d3.csv("test.csv", function(error,data) {
        data.forEach(function(d) {
            //console.log(Object.prototype.toString.call(d.TEST_DATE));
            a = d.TEST_DATE.split(" ",1);
            b=a[0].split("/");
            f_date = b[0].concat("-").concat(b[1]).concat("-").concat(b[2]);
            console.log(d.f_date);          
            console.log(f_date); 
             var date = new Date(f_date);
             var day = date.getDay();
            var month = date.getMonth()+1;
            var year = date.getFullYear();
            console.log("Day:"+day+" ,Month:"+month+" ,Year:"+year); 

            if(data.TEST_ACTIVITY == keys){
                console.log("check:"+keys);
                groupByMonth = d3.nest()
                .key(function(d){return year; })
                .key(function(d) {return month; })
                .rollup(function(v) { return d3.mean(v, function(d) { return d.TEST_DURATION; }); })
                .entries(data);
                console.log("Grouped Values by Month::"+JSON.stringify(groupByMonth));
            }   

            //finding the count of no. of ids per activity using Test_ID
        countByActivties=  d3.nest()
           .key(function(d) { return d.TEST_ACTIVITY; })
          .rollup(function(v) { return v.length; })
          .entries(data);
        console.log("Count of activities based on test-id::"+JSON.stringify(countByActivties));

        //sorting the data in descending order and find the top keys
        keys = countByActivties
        .sort(function(a, b){return b.values-a.values})
        .slice(0,6)
        .map(function(d){ return d.key;})
        console.log("Keys::"+keys);
</code></pre>

<p>I am getting the following error:</p>

<pre><code>Grouped Values by Month::[{"key":"2016","values":[{"key":"9","values":2.2857142857142856}]}]
</code></pre>

<p>Its output for example should be like:</p>

<pre><code>Grouped Values by Month::[{"key":"2016","values":[{"key":"9","values":6}]}]
</code></pre>

<p>and rest the same for other months as well.</p>

<p>Any idea??</p>