Skip to content

Aggregation Median incorrect calculation #4582

Description

@timsun28

Describe the bug

The current implementation of calculation of the median is incorrect.
This bug has been report before here, and it was fixed in the following commit.

However, if you now go to the current file the old version is back again.

Your minimal, reproducible example

https://codesandbox.io/s/exciting-sun-ddlcoz?file=/src/main.tsx

Steps to reproduce

Go to the given sandbox and click on the hand icon with More info. This will turn the table into a grouped view and will calculate the median for the age column and for the progress column.

Expected behavior

With the fixed data set it should be: age 15 and profile progress should be 20%. Those are the middle values of the three options. An alternative test case would be to add one more row and see if it calculates the average correctly of the two middle rows.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Any platform - version 8.7.1

react-table version

v8.7.1

TypeScript version

No response

Additional context

No response

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

Activity

  1. alex314159 commented on Apr 9, 2023

    @alex314159

    adding my voice - it's an important and simple fix, and the solution was provided

  2. KevinVandy commented on Apr 9, 2023

    @KevinVandy
    Member

    adding my voice - it's an important and simple fix, and the solution was provided

    Feel free to PR again

  3. alex314159 commented on Apr 12, 2023

    @alex314159

    Afraid I know nothing about TS, I use material-react-table through Clojurescript. Happy to PR the following if you think it works:

    const median: AggregationFn<any> = (columnId, leafRows) => {
      if (!leafRows.length) {
        return
      }
    
    //   let min = 0
    //   let max = 0
    //
    //   leafRows.forEach(row => {
    //     let value = row.getValue(columnId)
    //     if (typeof value === 'number') {
    //       min = Math.min(min, value)
    //       max = Math.max(max, value)
    //     }
    //   })
    //
    //   return (min + max) / 2
    
      let mid = Math.floor(leafRows.length / 2)
      let values = leafRows.map(d => d.getValue(columnId)).sort((a, b) => a - b)
      return values.length % 2 !== 0 ? values[mid] : (values[mid - 1] + values[mid]) / 2
    
    }
    
  4. KevinVandy commented on May 2, 2023

    @KevinVandy
    Member

    Afraid I know nothing about TS, I use material-react-table through Clojurescript. Happy to PR the following if you think it works:

    I had never heard of Clojurescript, but glad MRT is working for you. Looking into the median calc a bit more

  5. alex314159 commented on May 2, 2023

    @alex314159

    thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions