My data frame
is called 'mydata' and it has only 2 columns and looks as follows:
Date Sales
2016-07-01 51256
2016-08-01 75892
2016-09-01 67115
...
Running the following code str(mydata)
gives me the following:
'data.frame': 28 obs. of 2 variables:
$ DATE : chr "2016-07-01" "2016-08-01" "2016-09-01" ...
$ PKGREV: num 51256 75892 67115 ...
I have the following libraries loaded in R:
library(ggplot2)
library(dplyr)
library(scales)
library(ggthemes)
library(magrittr)
library(h2o)
library(timetk)
library(tidyquant)
I understand that I need to convert the 'Date' column into dates first before doing the xts
conversion.
From the 2 Stackoverflow questions below (see links below), I have made these 2 attempts:
Attempt 1:
mydata2 <- xts(mydata[,-1], order.by=mydata[,1])
Error message:
Error in xts(mydata[, -1], order.by = mydata[, 1]) :
order.by requires an appropriate time-based object
Attempt 2:
mydata2 <- xts(mydata[, -1], order.by=as.POSIXct(mydata$Date))
Error message:
Error in as.POSIXct.default(mydata$Date) :
do not know how to convert 'mydata$Date' to class “POSIXct”
StackOverflow questions consulted:
Converting a data frame to xts
Converting data.frame to xts order.by requires an appropriate time-based object
What am I missing here?