Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill `missings` in a vector using next valid observation

Is there any specific function for filling missings in an array? MWE:

x = [missing,missing,2,3,missing,4]
desired = [2,2,2,3,4,4]
like image 756
هنروقتان Avatar asked Dec 05 '25 02:12

هنروقتان


1 Answers

that functionality isn't in the Base language, but it is in Impute.jl. What you are looking is known as Next Observation carried Backward Imputation (nocb). you can do that with that julia package in the following way:

using Impute (if you don't have the package in your enviroment, add it)
x = [missing,missing,2,3,missing,4]
y = copy(x)
Impute.nocb!(x) #implace, overwrites x
z = Impute.nocb(y) #creates a new vector 

it also has other forms of data imputation, so if you are going to impute tabular data, that's the most secure way.

you could also implement your own nocb. this is the implementation used by Impute.jl, with some simplifications:

#implace version
function nocb!(data::AbstractVector{Union{T, Missing}}) where T
    @assert !all(ismissing, data)
    end_idx = findlast(!ismissing, data)
    count = 1
    for i in end_idx - 1:-1:firstindex(data)
        if ismissing(data[i])
            data[i] = data[i+1]
        else
            end_idx = i
            count = 1
        end
    end
    return data
end
#out of place version
nocb(x) = nocb!(copy(x))
like image 76
longemen3000 Avatar answered Dec 07 '25 21:12

longemen3000