mean function Summarizing data
Returns the mean of all values in the iterable.
This function ignores values that do not satisfy any of the following conditions:
- The value is not
null. - The value is not double.nan.
Useful for filtering and ignoring missing data in datasets.
If the iterable is empty or contains no valid values, this function
returns null.
Implementation
num? mean(Iterable<num?> iterable) {
num sum = 0;
var count = 0;
for (final value in iterable) {
if (value != null && !value.isNaN) {
++count;
sum += value;
}
}
return count > 0 ? sum / count : null;
}