withCategoricColumnFromFormula method
A new data frame with a categoric column generated from a formula.
Example:
print(petals.withHead(5));
print(petals.withHead(5).withCategoricColumnFromFormula(
name: "geom_mean",
formula: "sqrt(petal_width * petal_length)",
generator: (result) => result > 1 ? "large" : "small"
));
.--.------------.-----------.------------.
|id|petal_length|petal_width|species |
:--+------------+-----------+------------:
|1 |1.4 |0.2 |setosa |
|2 |1.4 |0.2 |setosa |
|3 |1.3 |0.2 |setosa |
|4 |1.5 |0.2 |setosa |
|51|4.7 |1.4 |versicolor |
'--'------------'-----------'------------'
.--.------------.-----------.------------.---------.
|id|petal_length|petal_width|species |geom_mean|
:--+------------+-----------+------------+---------:
|1 |1.4 |0.2 |setosa |small |
|2 |1.4 |0.2 |setosa |small |
|3 |1.3 |0.2 |setosa |small |
|4 |1.5 |0.2 |setosa |small |
|51|4.7 |1.4 |versicolor |large |
'--'------------'-----------'------------'---------'
Implementation
DataFrame withCategoricColumnFromFormula({
required String name,
required String formula,
required String Function(num) generator,
}) {
if (!_columnNameOkay(name)) {
throw PackhorseError.badColumnName(name);
}
return copy
..categoricColumns[name] =
CategoricColumn(_formulaResults(formula).map(generator));
}