logo

Calculating the Mean Excluding 0 or Missing Values in Julia 📂Julia

Calculating the Mean Excluding 0 or Missing Values in Julia

Overview

R language has options within functions like sum() or mean() to ignore missing values directly, whereas Julia lacks such options but actively employs Functional Programming approaches instead.

Code

julia> data = [0,1,2,3,0]
5-element Vector{Int64}:
 0
 1
 2
 3
 0

julia> sum(data) / length(data)
1.2

julia> sum(data) / sum(!iszero, data)
2.0

The top portion results in 1.2, dividing by the total number of samples including up to $0$, while the bottom portion achieves 2.0 by dividing by the number of samples counted excluding $0$ values using the !iszero function as an argument. What could be considered more powerful than R is that numerous exception handling functions like isnan(), isinf(), ismissing(), etc., can be used in the same manner without relying on the function’s own options, and customization is flexible.

While there isn’t a significant performance difference, if the return of is~ series functions is strictly Boolean, then it wouldn’t matter if the denominator’s sum() is changed to count().

Environment

  • OS: Windows
  • julia: v1.7.0