Calculating average of column

Calculating average of column

RakotoRakoto Posts: 8Questions: 2Answers: 0
edited April 2023 in Free community support

https://jsfiddle.net/lbriquet/k2zr5Lws/1/

Hi everyone,
I want to calculate average values of age based on the city like in the example above. Then I want to show this value on collapsed groups. For example, when we review Sidney, We see two values with an average value of 30. How can I show this value near the text of Sidney just like the total row number?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin
    Answer ✓

    Use cells().data() to get the data for the column in question, with the row selector set to be the rows for the group. Then you can use reduce() to calculate the sum:

    var ageTotal = rows
      .cells(rows.indexes(), 3)
      .data()
      .reduce((a, b) => a += 1*b, 0);
    var ageAvg = ageTotal / rows.count();
    

    Updated example.

    Allan

  • RakotoRakoto Posts: 8Questions: 2Answers: 0

    @allan Thanks Allan :) Have a nice day

Sign In or Register to comment.