Manipulating Time Series Data in R
Harrison Brown
Graduate Researcher in Geography
Regular time series:
NA valuesReal-world issues:
Aggregation:
Imputation:


na-dot functions from zoo:
zoo::na.fill()
zoo::na.locf()
zoo::na.approx()
observations
2017-01-01 NA
2017-01-02 2
2017-01-03 2
2017-01-04 2
2017-01-05 4
2017-01-06 2
2017-01-07 NA
2017-01-08 1
2017-01-09 2
2017-01-10 2
...
sum(is.na(observations))
[1] 23
observations
2017-01-01 NA
2017-01-02 2
2017-01-03 2
2017-01-04 2
2017-01-05 4
...
table(observations, useNA = 'ifany')
1 2 3 4 5 6 <NA>
43 29 17 13 1 2 23

observations_fill <-
na.fill(object = observations,
fill = 0)
table(observations_fill)
0 1 2 3 4 5 6
23 43 29 17 13 1 2
autoplot(observations_fill)

autoplot(scores)
Warning message:
Removed 12 row(s) containing
missing values (geom_path).

scores_locf <- na.locf(scores)
autoplot(scores_locf)

na.approx()

na.approx()

na.approx()

maunaloa_approx <-
na.approx(maunaloa_missing)
autoplot(maunaloa_approx) +
labs(
x = "Index",
y = "CO2 Concentration",
title = "Approximated Data Points"
)

Manipulating Time Series Data in R