0

I found responses to this very question answered on SO but I still can't get my specific case to work. I am trying to create a time-series object for use in a time series model. I've converted the date values in my dataframe to "Date" column (%Y-%m-%d) types yet I keep getting the error "Error in xts(x1_train[, -1], order.by = x1_train[, 1]) : order.by requires an appropriate time-based object". I've also tried other version by converting the date column by using as.Date or as.POSIXct() and it gives me the following error "Error in as.Date.default(x, ...) : do not know how to convert 'x' to class “Date”".

# date convert from CHR to DATE
df$rev_month <- as.Date(df$start_date, "%Y-%m-%d")

# 2 versions of attempts that return errors
x1_train_ts <- xts(x1_train[,-1], order.by=(x1_train[,1])
x1_train_ts <- xts(x1_train[,-1], order.by=as.Date(x1_train[,1], "%Y-%m-%d"))

Neither work although I am sure Date column is a Date format. Any direction is greatly appreciated.

# updated to include test dataframe
test <- data.frame("c_id" = c("none","none","none","none","none",
"none","none","none","none","none","none","none","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101", "none","none","none","none","none","none","none","none","none","none","none","none","none","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111"), "partner_id" = c("1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9"), "rev_month" = as.Date(c("2015-12-01","2016-01-01","2016-02-01","2016-03-01","2016-04-01","2016-05-01","2016-06-01","2016-07-01","2016-08-01", "2016-09-01","2016-10-01","2016-11-01","2016-12-01","2017-01-01","2017-02-01","2017-03-01","2017-04-01","2017-05-01","2017-06-01","2017-07-01","2017-08-01","2017-09-01","2017-10-01","2017-11-01","2017-12-01","2018-01-01","2018-02-01","2018-03-01","2018-04-01","2018-05-01","2018-06-01","2018-07-01","2018-08-01","2018-09-01","2018-10-01","2018-11-01","2018-12-01", "2017-01-01","2017-01-01","2017-02-01","2017-03-01","2017-04-01","2017-05-01","2017-06-01","2017-07-01","2017-08-01", "2017-09-01","2017-10-01","2017-11-01","2017-12-01","2018-01-01","2018-02-01","2018-03-01","2018-04-01","2018-05-01","2018-06-01","2018-07-01","2018-08-01","2018-09-01","2018-10-01","2018-11-01","2018-12-01","2019-01-01","2019-02-01","2019-03-01","2019-04-01","2019-05-01","2019-06-01","2019-07-01","2019-08-01","2019-09-01","2019-10-01","2019-11-01","2019-12-01", "2020-01-01", "2020-02-01", "2020-03-01")), "rev" = c(101.25, 102.25, 103.50, 103.75, 104.15, 104.25, 104.3, 105.00, 105.20, 105.60, 106.00, 106.10, 106.50, 101.50, 100.30, 107.50, 108.30, 108.45, 109.10, 110.10, 112.15, 112.45, 114.65, 115.00, 116.00, 116.50, 117.25, 117.85, 119.25, 119.95, 120.20, 121.50, 122.30, 122.40, 123.25, 123.75, 124.00, 101.25, 102.25, 103.50, 103.75, 104.15, 104.25, 104.3, 105.00, 105.20, 105.60, 106.00, 106.10, 106.50, 101.50, 100.30, 107.50, 108.30, 108.45, 109.10, 110.10, 112.15, 112.45, 114.65, 115.00, 116.00, 116.50, 117.25, 117.85, 119.25, 119.95, 120.20, 121.50, 122.30, 122.40, 123.25, 123.75, 124.00, 124.10, 125.35, 125.45), stingsAsFactors=FALSE)
bbal20
  • 113
  • 4
  • 11

1 Answers1

1

You have created a new column rev_month of date class, start_date is still of type character/factor. Instead of creating a new column, overwrite the existing one.

df$start_date <- as.Date(df$start_date, "%Y-%m-%d")

and then this should work provided start_date is the 1st column in your dataframe df.

x1_train_ts <- xts::xts(df[,-1], order.by= df$start_date)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • thank you for the response. Sorry so I did overwrite the date column(s) in the original datframe and can confirm they are Date column types before. x1_train which is just the univariate time series subset of my dataframe with just revenue and dates. This is tormenting me lol – bbal20 Nov 18 '20 at 01:42
  • So my answer does work for you, right? You should not have `rev_month` column in your dataframe. – Ronak Shah Nov 18 '20 at 01:47
  • No unfortunately it doesn't work. Still giving me the same error as if it doesn't recognize the proper date format – bbal20 Nov 18 '20 at 03:15
  • @bbal20 Please provide an example of your data then. Update your post with `dput(head(df))`. – Ronak Shah Nov 18 '20 at 03:17
  • I just updated the original post to include a sample dataframe. This code is part of a loop so ultimately each date will be unique for a particular partner_id but I'm just trying to break it down to fix my errors. – bbal20 Nov 18 '20 at 03:49
  • So I giess only relevant column is `rev` in your data ? This works for the data shared. `xts::xts(test$rev, order.by= test$rev_month)`. Would this work for your case? – Ronak Shah Nov 18 '20 at 03:55
  • So when I do 'x1_train_ts <- xts::xts(test$rev, order.by=test$rev_month)' it worked but when I take the steps of filtering the dataframe like 'x1_train <- test[test$c_id == "none", c(3,4)]' and then 'x1_train_ts <- xts::xts(x1_train[-1], order.by=x1_train[1])' it does not work.I guess I can find a workaround thank you for your help. – bbal20 Nov 18 '20 at 04:10
  • Why are you referring columns by index and not names? Use `[[]]` : `xts::xts(x1_train[-1], order.by=x1_train[[1]])` should work. – Ronak Shah Nov 18 '20 at 04:37