1

I have a time series table named ff5 imported into R using read.csv with the date column in format of "YYYYMMDD".

I installed the xts package to better handle time series data. I tried to use the below code to convert the original data ff5 to xts format:

library(xts)
ff5_xts <- xts(ff5, order.by = as.Date(ff5["date"], "%Y%m%d"))

And I got this error message:

Error in as.Date.default(x, ...) : do not know how to convert 'x' to class “Date”

I tried a few other ways with or without xts but could not figure out how to convert this original data into time series.

Would appreciate any help!

Adam Smith
  • 2,584
  • 2
  • 20
  • 34
Alex
  • 13
  • 4
  • 1
    Can you add as sample of your data in order to allow other to reproduce your error? – Dave2e Jun 23 '18 at 19:04
  • Following up on @Dave2e's good comment, you should review this: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Adam Smith Jun 23 '18 at 19:22
  • 1
    Although an example would be nice, this error is quite obvious. Read up on the difference between "[" and "[[". You needed to use: `as.Date(ff5[["date"]], "%Y%m%d")` – IRTFM Jun 23 '18 at 20:07
  • Because the error is "obvious" (indicative of a novice user), the sample data is of even greater importance. – Adam Smith Jun 23 '18 at 21:01
  • 1
    Yes I am quite new to R. Thanks everyone for your helpful comments. I should have read a bit more about the rules of using this forum. – Alex Jun 24 '18 at 17:13

1 Answers1

1

Does this work?

ff5 <- data.frame(date=c("20180615", "20180617", "20180616"))
ff5$date <- as.Date(ff5$date, "%Y%m%d")
library(xts)
ff5_xts <- xts(ff5, order.by = ff5$date)
ff5_xts
           date        
2018-06-15 "2018-06-15"
2018-06-16 "2018-06-16"
2018-06-17 "2018-06-17"
Adam Smith
  • 2,584
  • 2
  • 20
  • 34