It seems like you haven't converted the date column into a valid date variable. I've included an example below that might be helpful.
Packages
- It seems like you’re using
tibbletime, but tibbletime has been
replaced with tsibble. See the README on the GitHub repository of
tibbletime for the annoucement.
-
lubridate is a helpful package for handling dates.
library(tsibble)
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:tsibble':
##
## interval, new_interval
## The following object is masked from 'package:base':
##
## date
Loading data as an example
When posting a question, it can be helpful to post the data in a form
that others can replicate. You can get output like I have below, by
using the command dput.
ex_data <- structure(list(date = c(
"01/02/2003 01:21:18", "01/03/2003 01:21:18",
"01/04/2003 01:21:18", "01/05/2003 01:21:18", "01/06/2003 01:21:18",
"01/07/2003 01:21:18", "01/08/2003 01:21:18", "01/09/2003 01:21:18",
"01/10/2003 01:21:18", "01/11/2003 01:21:18", "01/12/2003 01:21:18",
"01/01/2004 01:21:18", "01/02/2004 01:21:18", "01/03/2004 01:21:18",
"01/04/2004 01:21:18", "01/05/2004 01:21:18", "01/06/2004 01:21:18"
), gwt = c(
0.41, 0.35, 0.22, 0.03, -0.05, -0.09, -0.1, -0.02,
0.04, 0.11, 0.08, 0, -0.19, -0.34, -0.46, -0.59, -0.63
)), row.names = c(
NA,
-17L
), class = "data.frame")
print(head(ex_data))
## date gwt
## 1 01/02/2003 01:21:18 0.41
## 2 01/03/2003 01:21:18 0.35
## 3 01/04/2003 01:21:18 0.22
## 4 01/05/2003 01:21:18 0.03
## 5 01/06/2003 01:21:18 -0.05
## 6 01/07/2003 01:21:18 -0.09
Handling dates
We need to convert the date column into a valid date object. We can
use mdy_hms from the lubridate package to accomplish this. However,
in the example data you posted, it seems like the hour, minute, seconds
portion of the date was the same for each row. In this case,
as_tsibble will throw an error. See the FAQ on error “Can’t obtain
the interval due to the mismatched index
class.”.
So, we can truncate the date-time object into just a date object (i.e.,
we ignore the hours, minutes and seconds) using as_date from the
lubridate package.
ex_data$date_num <- as_date(mdy_hms(ex_data$date))
print(head(ex_data))
## date gwt date_num
## 1 01/02/2003 01:21:18 0.41 2003-01-02
## 2 01/03/2003 01:21:18 0.35 2003-01-03
## 3 01/04/2003 01:21:18 0.22 2003-01-04
## 4 01/05/2003 01:21:18 0.03 2003-01-05
## 5 01/06/2003 01:21:18 -0.05 2003-01-06
## 6 01/07/2003 01:21:18 -0.09 2003-01-07
Now we have a valid date object to use as an index.
ex_tsibble <- as_tsibble(ex_data, index = date_num)
print(ex_tsibble)
## # A tsibble: 17 x 3 [1D]
## date gwt date_num
## <chr> <dbl> <date>
## 1 01/02/2003 01:21:18 0.41 2003-01-02
## 2 01/03/2003 01:21:18 0.35 2003-01-03
## 3 01/04/2003 01:21:18 0.22 2003-01-04
## 4 01/05/2003 01:21:18 0.03 2003-01-05
## 5 01/06/2003 01:21:18 -0.05 2003-01-06
## 6 01/07/2003 01:21:18 -0.09 2003-01-07
## 7 01/08/2003 01:21:18 -0.1 2003-01-08
## 8 01/09/2003 01:21:18 -0.02 2003-01-09
## 9 01/10/2003 01:21:18 0.04 2003-01-10
## 10 01/11/2003 01:21:18 0.11 2003-01-11
## 11 01/12/2003 01:21:18 0.08 2003-01-12
## 12 01/01/2004 01:21:18 0 2004-01-01
## 13 01/02/2004 01:21:18 -0.19 2004-01-02
## 14 01/03/2004 01:21:18 -0.34 2004-01-03
## 15 01/04/2004 01:21:18 -0.46 2004-01-04
## 16 01/05/2004 01:21:18 -0.59 2004-01-05
## 17 01/06/2004 01:21:18 -0.63 2004-01-06