Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mode() aggregation function to find most common unique value. #5864

Open
jeremy-code opened this issue Jan 11, 2025 · 0 comments
Open

Add mode() aggregation function to find most common unique value. #5864

jeremy-code opened this issue Jan 11, 2025 · 0 comments

Comments

@jeremy-code
Copy link

The statistical mode is a very common metric for data and very useful with nominal/categorical data. For example, say a table is of days with categories ["Sunny", "Cloudy", "Rainy"] -- knowing most days are "Rainy" is more useful than using unique() and knowing that the weather can be those categories.

A rough implementation of one can be seen below. The reason for the weird Array.from(counts).find(...) is that most implementations of mode from what I can tell tend to prefer finding the first element with the maximum count when there is a tie. I am open to other implementations if this isn't a concern.

const mode: AggregationFn<any> = (columnId, leafRows) => {
  if (!leafRows.length) {
    return
  }

  let maxCount = 0
  const counts = leafRows.reduce((counts, row) => {
    const value = row.getValue(columnId)
    const valueCount = (counts.get(value) ?? 0) + 1
    maxCount = Math.max(maxCount, valueCount)
    return counts.set(value, valueCount)
  }, new Map<unknown, number>())

  return Array.from(counts).find(([, count]) => count === maxCount)![0]
}

And here is a StackBlitz implementation showing it passes unit tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant