Nanmean for 3d array

Does julia have the nanmean function for 3d array, just like what in MATLAB (Not recommended) Mean, ignoring NaN values - MATLAB nanmean?

You can use the nanmean function from NaNStatistics.jl

https://brenhinkeller.github.io/NaNStatistics.jl/dev/#NaNStatistics.nanmean-Tuple{Any}

1 Like

This package works for Nan. But not for missing. Any solution?

using NaNStatistics
A = [1:10; fill(missing,10)]
nanmean(A)
# missing

You can define your own version of missmean which checks for nans and missings and works along any dimension like this:

misssum((s,n), x) = (x===missing || isnan(x)) ? (s,n) : (s+x, n+1)
division((s,n)) = s/n
missmean(a;dims=:) = division.(reduce(misssum, a, init = (zero(eltype(a)), 0), dims=dims))

#And then call it on your data
A = [1.0:10.0 fill(missing,10)]
A[1,2] = NaN
missmean(A, dims=2)
1 Like