mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 20:47:45 +00:00
Spreadsheet: Implement the mode function
My statistics course has gotten to me :^)
This commit is contained in:
parent
7851151cfb
commit
c4013f72a3
1 changed files with 45 additions and 2 deletions
|
@ -351,6 +351,29 @@ function variance(cells) {
|
||||||
return (count * squares - sums * sums) / count;
|
return (count * squares - sums * sums) / count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function mode(cells) {
|
||||||
|
const counts = numericReduce(
|
||||||
|
(map, x) => {
|
||||||
|
if (!map.has(x)) map.set(x, 0);
|
||||||
|
map.set(x, map.get(x) + 1);
|
||||||
|
return map;
|
||||||
|
},
|
||||||
|
new Map(),
|
||||||
|
cells
|
||||||
|
);
|
||||||
|
|
||||||
|
let mostCommonValue = undefined;
|
||||||
|
let mostCommonCount = -1;
|
||||||
|
counts.forEach((count, value) => {
|
||||||
|
if (count > mostCommonCount) {
|
||||||
|
mostCommonCount = count;
|
||||||
|
mostCommonValue = value;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return mostCommonValue;
|
||||||
|
}
|
||||||
|
|
||||||
function stddev(cells) {
|
function stddev(cells) {
|
||||||
return Math.sqrt(variance(cells));
|
return Math.sqrt(variance(cells));
|
||||||
}
|
}
|
||||||
|
@ -727,15 +750,24 @@ variance.__documentation = JSON.stringify({
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
E4: {
|
E4: {
|
||||||
|
kind: "Formula",
|
||||||
|
source: "mode(R`A0:C4`)",
|
||||||
|
value: "1",
|
||||||
|
type: "Numeric",
|
||||||
|
type_metadata: {
|
||||||
|
format: "mode: %d",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
E5: {
|
||||||
kind: "Formula",
|
kind: "Formula",
|
||||||
source: "count(R`A0:C4`)",
|
source: "count(R`A0:C4`)",
|
||||||
value: "15",
|
value: "12",
|
||||||
type: "Numeric",
|
type: "Numeric",
|
||||||
type_metadata: {
|
type_metadata: {
|
||||||
format: "count: %d",
|
format: "count: %d",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
E5: {
|
E6: {
|
||||||
kind: "Formula",
|
kind: "Formula",
|
||||||
source: "sum(R`A0:C4`)",
|
source: "sum(R`A0:C4`)",
|
||||||
value: "18",
|
value: "18",
|
||||||
|
@ -771,6 +803,17 @@ variance.__documentation = JSON.stringify({
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
mode.__documentation = JSON.stringify({
|
||||||
|
name: "mode",
|
||||||
|
argc: 1,
|
||||||
|
argnames: ["cell names"],
|
||||||
|
doc: "Calculates the mode of the numeric values in the given range of cells, i.e. the value that appears most often",
|
||||||
|
examples: {
|
||||||
|
'mode(range("A2", "A14"))':
|
||||||
|
"Calculate the mode of the values in A2:A14, [Click to view](spreadsheet://example/variance#simple)",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
stddev.__documentation = JSON.stringify({
|
stddev.__documentation = JSON.stringify({
|
||||||
name: "stddev",
|
name: "stddev",
|
||||||
argc: 1,
|
argc: 1,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue