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]
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))
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