UNPKG

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

31 lines (30 loc) 1.64 kB
import { transpose, extractMetrics, split, clone, sortBy, htmlTable,filterKeys } from './tools.js' import { Counter } from "../../utils/counter.js"; import { TableData } from '../table-data/index.js' export default class SimpleTable extends TableData { static counter = new Counter('table') Table = SimpleTable constructor(data = {}, options = {}) { options = { minK: 0, colFilter: [], name: SimpleTable.counter.getName(options.name), ...options } super(data, options) if (this.k < this.options.minK) throw new Error(`At least ${this.options.minK} samples required`); } get name() { return this.options.name } descriptive(...metricNames) { return extractMetrics(this.columns, metricNames) } clone(name, colFilter = []) { return clone(name, colFilter, this) } splitBy(colName, labels) { return split(colName, this, labels) } sortBy(colName, asc = true) { sortBy(this, colName, asc); return this; } transpose(colNames = []) { return new SimpleTable(transpose(this, colNames), { name: (this.name + 'transposed') }) } rows(withKeys = true) { return transpose(this, [], withKeys ? this.colNames : []) } get json() { const obj = {} this.colValues.forEach(({ name, values }) => obj[name] = values) return obj } htmlTable(colFilter=[], options = {}) { const headers = filterKeys(this.colNames,colFilter) const rows = Object.values(transpose(this,headers)) options = { firstColHeader: false, header: `${this.name} data`, ...options } // style = 'text-align:left;',fixed=8 return htmlTable(rows, headers, options) } }