diff --git a/src/counts.jl b/src/counts.jl index d7485fd3..7cbb7a0a 100644 --- a/src/counts.jl +++ b/src/counts.jl @@ -450,5 +450,13 @@ Return a dictionary mapping each unique value in `x` to its proportion in `x`. If a vector of weights `wv` is provided, the proportion of weights is computed rather than the proportion of raw counts. """ -proportionmap(x::AbstractArray) = _normalize_countmap(countmap(x), length(x)) +function proportionmap(x) + countm = Dict{eltype(x), Int}() + n = 0 + for y in x + countm[y] = get(countm, y, 0) + 1 + n += 1 + end + _normalize_countmap(countm, n) +end proportionmap(x::AbstractArray, wv::AbstractWeights) = _normalize_countmap(countmap(x, wv), sum(wv)) diff --git a/test/counts.jl b/test/counts.jl index 262f2bbd..fa30969c 100644 --- a/test/counts.jl +++ b/test/counts.jl @@ -209,3 +209,10 @@ if VERSION >= v"1.9.0-DEV" # countmap and proportionmap only support the :dict algorithm for weighted sums. end end + +@testset "proportionmap with iterator" begin + a = [1, 2, 3, 4] + b =[true, true ,false, false, true, false] + @test proportionmap(skipmissing(a)) == Dict(1 => 0.25, 2 => 0.25, 3 => 0.25, 4 => 0.25) + @test proportionmap(skipmissing(b)) == Dict(true => 0.5, false => 0.5) +end