Arithmetic operations on multi-dimensional arrays with Missings

Given arrays with missing values, I am unable to workout the mean of a mutli-dimensional array over a given region

For example,

x = convert(Array{Union{Float64,Missing}}, rand(2,2))
x[1] = missing

works as expected. However computing the mean over a given region, say mean(skipmissing(x), 2) returns an error indicating that the relevant method has not been defined.

Other than writing my own method (which I cannot do at the moment), is there a way to calculate the mean of a mutli-dimensional array with missing values over a given region?

Perhaps something like

julia> x
2×2 Array{Union{Missing, Float64},2}:
  missing  0.848082
 0.125747  0.481677

julia> [mean(skipmissing(x[i, :])) for i in 1:size(x, 2)]
2-element Array{Float64,1}:

julia> hcat([mean(skipmissing(x[i, :])) for i in 1:size(x, 2)])
2×1 Array{Float64,2}:

where the hcat call is used to make the result into a Matrix (just like mean(x, 2) does) instead of a Vector.

Or using mapslices:


although this is probably slower than a hand-coded function.

@fredrikekre, @fabiangans: both solutions worked. Thanks

I just submitted a PR to incorporate a generic version of this into base.