0

NOTE: PROBLEM RESOLVED IN THE COMMENTS BELOW

I'm getting the following error when trying to turn a data.frame into xts following the answer in found here.

Error in .xts(DA[, 3:6], index = as.POSIXct(DAINDEX, format = "%m/%d/%Y %H:%M:%S", : index is not in increasing order

I've not been able to find much on this error or how to resolve it, so any help towards that would be greatly appreciated.

The data is daily S&P 500 in a comma delimited format with the following columns: "Date" "Time" "Open" "High" "Low" "Close".

Below is the code:

DA <- read.csv("SNP.csv", header = TRUE, stringsAsFactors = FALSE)
DAINDEX <- paste(DA$Date, DA$Time, sep = " ")
Data.hist <- .xts(DA[,3:6], index = as.POSIXct(DAINDEX, format = "%m/%d/%Y %H:%M:%S", tzone = "GMT"))

As requested, some lines of the data

structure(list(Date = c("5/20/2016", "5/19/2016", "5/18/2016", 
"5/17/2016", "5/16/2016", "5/13/2016"), Time = c("0:00:00", "0:00:00", 
"0:00:00", "0:00:00", "0:00:00", "0:00:00"), Open = c(2041.880005, 
2044.209961, 2044.380005, 2065.040039, 2046.530029, 2062.5), 
High = c(2058.350098, 2044.209961, 2060.610107, 2065.689941, 
2071.879883, 2066.790039), Low = c(2041.880005, 2025.910034, 
2034.48999, 2040.819946, 2046.530029, 2043.130005), Close = c(2052.320068, 
2040.040039, 2047.630005, 2047.209961, 2066.659912, 2046.609985
)), .Names = c("Date", "Time", "Open", "High", "Low", "Close"
), row.names = c(NA, 6L), class = "data.frame")

The above is the output of dput(head(DA))

Community
  • 1
  • 1
youjustreadthis
  • 622
  • 3
  • 9
  • 24
  • 1
    Could you please provide a few rows of your data here? Use `dput(head(DA))` ... – Martin Schmelzer May 23 '16 at 20:16
  • @MartinDabbelJuSmelter will do as soon as I get back to my computer! – youjustreadthis May 23 '16 at 21:21
  • @MartinDabbelJuSmelter Please see the edited question. Actually using dput made me realize that the data was sorted from newest to oldest rather than vice verca. If you'd like please leave an answer so I can accept it. Thanks a lot for your help! – youjustreadthis May 24 '16 at 07:48

1 Answers1

0

The easiest thing to do is use the regular xts constructor instead of .xts. It will check if the index is sorted correctly, and sort the index and data, if necessary.

Data.hist <- xts(DA[,3:6], as.POSIXct(DAINDEX, "%m/%d/%Y %H:%M:%S", "GMT"))
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418