I have a data frame containing a time series of monthly data, with some missing values.
dates <- seq(
as.Date("2010-01-01"), as.Date("2017-12-01"), "1 month"
)
n_dates <- length(dates)
dates <- dates[runif(n_dates) < 0.5]
time_data <- data.frame(
date = dates,
value = rnorm(length(dates))
)
## date value
## 1 2010-02-01 1.3625419
## 2 2010-06-01 0.1512481
## etc.
In order do be able to make use of time series forecasting functionality in, e.g., forecast
, I'd like to convert this to a ts
object.
The dumb way to do this is to create a regular set of monthly dates over the whole time period, then left join back to the original data.
library(dplyr)
first_date <- min(time_data$date)
last_date <- max(time_data$date)
full_dates <- data.frame(
date = seq(first_date, last_date, "1 month")
)
extended_time_data <- left_join(full_dates, time_data, by = "date")
## date value
## 1 2010-02-01 1.3625419
## 2 2010-03-01 NA
## etc.
Now I can create the time series using ts()
.
library(lubridate)
time_series <- ts(
extended_time_data$value,
start = c(year(first_date), month(first_date)),
frequency = 12
)
For such a simple task, this is long-winded and pretty gross.
I also looked into first converting to xts
, and using a convertor from the timetk
package, but nothing jumped out at me as an easier way.
This question is a dupe of How to create time series with missing datetime values, but the answer there was even fuzzier.
How do I create a ts
object from a time series with missing values?
n_dates
Try withexpand
time_data %>% expand(date = seq(min(date),max(date), by = "1 month"), select(., everything(), -date))
– akrunn_dates
should belength(seq( as.Date("2010-01-01"), as.Date("2017-12-01"), "1 month" ))
– Chriss Paullubridate
package to accessyear
/month
etc in your example code. – thelatemail