Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: How to count efficiently the number of missings in a `Vector{Union{T, Missing}}`

Tags:

julia

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?

like image 466
xiaodai Avatar asked Jan 26 '23 13:01

xiaodai


1 Answers

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.

like image 152
carstenbauer Avatar answered May 17 '23 08:05

carstenbauer