Skip to content

Instantly share code, notes, and snippets.

@geotheory
Created October 22, 2021 10:13
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 geotheory/181074295369fcea891c55798396d913 to your computer and use it in GitHub Desktop.
Save geotheory/181074295369fcea891c55798396d913 to your computer and use it in GitHub Desktop.
# identify and fill missing data points in timeseries data with regular-periodicity
require(padr)
#> Loading required package: padr
df <- data.frame(date = as.Date('2016-04-01') + 3*c(0,1,3,5,6), val = sample(5)) |>
print()
#> date val
#> 1 2016-04-01 3
#> 2 2016-04-04 1
#> 3 2016-04-10 2
#> 4 2016-04-16 4
#> 5 2016-04-19 5
pad(df, interval = '3 days') |> print() |>
fill_by_value(val, value = 0) |> print()
#> date val
#> 1 2016-04-01 3
#> 2 2016-04-04 1
#> 3 2016-04-07 NA
#> 4 2016-04-10 2
#> 5 2016-04-13 NA
#> 6 2016-04-16 4
#> 7 2016-04-19 5
#> date val
#> 1 2016-04-01 3
#> 2 2016-04-04 1
#> 3 2016-04-07 0
#> 4 2016-04-10 2
#> 5 2016-04-13 0
#> 6 2016-04-16 4
#> 7 2016-04-19 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment