Reputation: 1013
I have a 3x3 dataframe. One of its rows contains Date and Time information. When I convert the dataframe to an xts object, the conversion strips the Time from the data, leaving only the Date behind. I must have the wrong code. Yet, I believe I followed the instructions correctly. Any help would be greatly welcome.
library(xts)
library(data.table)
DATSB <- structure(list(DateTime = c("3/28/2016 20:37", "3/28/2016 20:36","3/28/2016 20:35"), Last = c(1221.7, 1221.8, 1221.9), Volume = c(14L,2L, 22L)), .Names = c("DateTime", "Last", "Volume"), row.names = c(NA,3L), class = "data.frame")
setDF(DATSB)
DATSB$DateTime <- strptime(DATSB$DateTime, format = "%m/%d/%Y %H:%M")
DATSBxts <- as.xts(DATSB[, -1], order.by = as.Date(DATSB$DateTime, "%Y/%m/%d %H:%M"))
Output for data.frame DATSB:
DateTime Last Volume
1 2016-03-28 20:37:00 1221.7 14
2 2016-03-28 20:36:00 1221.8 2
3 2016-03-28 20:35:00 1221.9 22
Output for xts object DATSBxts:
Last Volume
2016-03-28 1221.7 14
2016-03-28 1221.8 2
2016-03-28 1221.9 22
Upvotes: 1
Views: 360
Reputation: 1013
Remove as.Date like this DATSBxts <- as.xts(DATSB[, -1], order.by = DATSB$DateTime)
Upvotes: 1