Skip to content
Prev 7053 / 15274 Next

non-standard time-format conversion from data.frame to xts

On Wed, Dec 8, 2010 at 9:02 AM, Andres Susrud <andres.susrud at gmail.com> wrote:
Assuming your data frame is this we can use read.zoo to read it into
zoo giving it a chron "times" class:

DF <- structure(list(Time = structure(1:5, .Label = c("7:10:03 AM",
    "7:10:36 AM", "7:11:07 AM", "7:11:48 AM", "7:12:25 AM"), class = "factor"),
    Bid = c(6118.5, 6118.5, 6119.5, 6119, 6119), Offer = c(6119.5,
    6119.5, 6119.5, 6120, 6119.5)), .Names = c("Time", "Bid",
    "Offer"), class = "data.frame", row.names = c(NA, -5L))

library(zoo)
library(chron)
z <- read.zoo(DF, FUN = function(x) times(as.chron(paste("1970-01-01", x),
   format = "%Y-%m-%d %H:%M:%S %p")))
z

If you need it in zoo then you now have it with "times" class which
seems the most appropriate for that data.

xts does not support chron "times" class but if you need it in xts you
could convert it to some date/time class and then use as.xts. e.g.

as.xts(aggregate(z, chron))