als-statistics
Version:
Modular JS statistics toolkit for Node.js and the browser: descriptive stats, correlations (Pearson/Spearman/Kendall), t-tests & ANOVA (Student/Welch), reliability (Cronbach’s alpha), regression (linear/logistic), clustering (DBSCAN/HDBSCAN), and table/co
20 lines (19 loc) • 846 B
JavaScript
export function split(colName, table, labels = {}) {
const { columns, name, Table: { Statistics } } = table
const col = columns[colName]
if (!col) throw new Error(`Column ${colName} does not exist`)
const statistics = new Statistics(`SplitedBy${colName}`)
const groups = {}
Object.entries(columns[colName].values).sort((a, b) => a[1] - b[1]).forEach(([i, v]) => {
(groups[v] ? groups[v] : groups[v] = new Set()).add(Number(i))
})
for (let groupValue in groups) {
const tName = labels[groupValue] || name + groupValue
const newColumns = {}, indexes = groups[groupValue]
for (let colName in columns) {
newColumns[colName] = columns[colName].values.filter((v, i) => indexes.has(i))
}
statistics.addTable(newColumns, { name: tName })
}
return statistics
}