Add `mode()` aggregation function to find most common unique value.
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 28.4k
- Forks
- 3.6k
- Avg merge
- 1d 14h
- Merged PRs (30d)
- 10
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the aggregation-function APIs and the StackBlitz example linked in the issue, including mode.test.ts. Add mode() as an available aggregation function, preserving the first value on ties, and ensure the unit tests cover the described behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- data
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100