Consider
x = rand([missing, rand(Int, 100)...], 1_000_000)
which yields typeof(x)
= Array{Union{Missing, Int64},1}
.
What's the most efficient way to count the number of missings in x
?
The cleanest way is probably just
count(ismissing, x)
Simple, easy to remember, and fast
Since you're asking for the "most efficient" way, let me give some benchmark results. It is slightly faster than @xiaodai's answer, and as fast as a simple loop implementation:
julia> @btime count($ismissing,$x);
278.499 μs (0 allocations: 0 bytes)
julia> @btime mapreduce($ismissing, $+, $x);
293.901 μs (0 allocations: 0 bytes)
julia> @btime count_missing($x)
278.499 μs (0 allocations: 0 bytes)
where
julia> function count_missing(x)
c = 0
@inbounds for i in eachindex(x)
if ismissing(x[i])
c += 1
end
end
return c
end
Abstraction for no cost, just the way you'd want it to be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With