Skip to content

Instantly share code, notes, and snippets.

@flare9x
Last active July 8, 2018 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flare9x/4fd0b2d316e94ea85edde895e2ba83ca to your computer and use it in GitHub Desktop.
Save flare9x/4fd0b2d316e94ea85edde895e2ba83ca to your computer and use it in GitHub Desktop.
Carry forward previous value forward to fill NAs
# NA LOCF
# First convert missing to 0 (just easier to work with)
# Pull data to array
example_array = df[:col]
example_array = Float64.(collect(Missings.replace(example_array, 0.0)))
# NA LOCF Function
function locf(x::Array{Float64})
dx = zeros(x)
for i in 2:length(x)
if i == 2
# fill index 1
dx[i-1] = x[i-1]
end
if (x[i] == 0.0 && x[i-1] != 0.0)
dx[i] = x[i-1]
else
dx[i] = x[i]
end
if (x[i] == 0.0 && x[i-1] == 0.0)
dx[i] = dx[i-1]
end
end
return dx
end
# run na locf function
out = locf(example_array)
# Fill 1x gap
function locf(x::Array{Float64})
dx = zeros(x)
for i in 2:length(x)-1
if x[i+1] > 0 && x[i] == 0.0
dx[i+1] = x[i+1]
end
if dx[i] == 0
dx[i] = dx[i-1]
end
end
return dx
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment