JontroPothon
JontroPothon

Reputation: 600

Fixing time series class in timetk

I have this data,

library(timetk)
library(tidyverse)

time <- c(122019, 012020, 022020, 032020, 042020, 052020, 062020, 072020, 082020, 092020, 102020, 112020)
value <- c(100, 234, 269, 432, 150, 455, 66, 223, 333, 212, 111, 44)
data <- data.frame(time, value)

If I plot the time series using,

data %>% plot_time_series(time, value)

It works but incorrectly shows the time axis. I am wondering how to appropriately show time in the horizontal axis correctly.

To see if the package is recognising the timestamp automatically,

data %>% tk_get_timeseries_variables()

It shows,

character(0)

I guess the time series is missing. That's why timetk is not recognizing the timestamps. Anyone here to help me fixing the problem?

Upvotes: 0

Views: 52

Answers (1)

royr2
royr2

Reputation: 2299

The time column is just a numeric vector. By default, it won't be converted to any other format that reflects time series data.

One way to do this would be to create the time variable as a character vector and then add the day to it and convert it to a Date object.

time <- c("122019", "012020", "022020", "032020", "042020", "052020", "062020", "072020", "082020", "092020", "102020", "112020")
value <- c(100, 234, 269, 432, 150, 455, 66, 223, 333, 212, 111, 44)

data <- data.frame(time = as.Date(paste0("01", time), format = "%d%m%Y"), value)
data %>% plot_time_series(time, value)

Now running data %>% tk_get_timeseries_variables() should time as the timeseries variable.

Upvotes: 1

Related Questions