Skip to content

Add a function for skippmissing and then "spread"ing the result #121

@pdeffebach

Description

@pdeffebach

This is something I have discussed elsewhere as being useful, and came up today on slack.

Problem

function standardize(x)
    mu = mean(x)
    sigma = std(x)
    return (x .- mu) ./ sigma
end
standardize([1,2,3, missing]

We can't use passmissing because this isn't an element-wise operation. We need an operation that applies skipmissing to the input, then applies the function, then "spreads" the result of the function to a vector of the same length as the output.

function skipmissing_then_collect(fun, args...)
    smargs = skipmissings(args...)
    res = fun(smargs...)
    out = Union{eltype(res), Missing}[missing for i in 1:length(first(args))]# assume all args vectors also
    res_counter = 1
    for i in eachindex(first(smargs))
        out[i] = res[res_counter] # can probably do fancy iteration stuff here
        res_counter += 1
    end
    out
end

This might solve a lot of problems in DataFrames as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions