将数据框转换为Zoo。 Posixlt的麻烦

时间:2014-04-12 01:55:30

标签: r dataframe zoo

我正在尝试将数据框转换为动物园对象,并且遇到了很多麻烦。无论我在read.zoo调用中做什么,我都会得到以下错误。我认为甚至没有必要列出时间戳的格式,因为它已经是Posixlt但事实并非如此。

你能指出我正确的方向吗?

Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format

这是数据样本

                   Date   Open   High    Low  Close
330 2014-01-03 15:00:00 544.95 545.10 544.80 544.86
331 2014-01-03 15:01:00 544.80 544.89 544.77 544.79
332 2014-01-03 15:02:00 544.84 544.90 544.79 544.80
333 2014-01-03 15:03:00 544.80 544.80 544.60 544.69
334 2014-01-03 15:04:00 544.75 544.80 544.66 544.75
335 2014-01-03 15:05:00 544.78 545.03 544.76 545.01

这是可重现的代码:

require (zoo)

data <- structure(list(Date = structure(list(sec = c(0, 0, 0, 0, 0, 0
), min = 0:5, hour = c(15L, 15L, 15L, 15L, 15L, 15L), mday = c(3L, 
3L, 3L, 3L, 3L, 3L), mon = c(0L, 0L, 0L, 0L, 0L, 0L), year = c(114L, 
114L, 114L, 114L, 114L, 114L), wday = c(5L, 5L, 5L, 5L, 5L, 5L
), yday = c(2L, 2L, 2L, 2L, 2L, 2L), isdst = c(0L, 0L, 0L, 0L, 
0L, 0L)), .Names = c("sec", "min", "hour", "mday", "mon", "year", 
"wday", "yday", "isdst"), class = c("POSIXlt", "POSIXt")), Open = c(544.95, 
544.8, 544.84, 544.8, 544.75, 544.78), High = c(545.1, 544.89, 
544.9, 544.8, 544.8, 545.03), Low = c(544.8, 544.77, 544.79, 
544.6, 544.66, 544.76), Close = c(544.86, 544.79, 544.8, 544.69, 
544.75, 545.01)), .Names = c("Date", "Open", "High", "Low", "Close"
), row.names = 330:335, class = "data.frame")

data <- read.zoo(data, header = TRUE, index = 1, tz="", format = "%Y-%m-%d %H:%M:%S")

1 个答案:

答案 0 :(得分:2)

假设在data <- structure(…

之后立即调用以下内容
read.zoo( transform( data, Date = as.POSIXct(Date) ), FUN = identity )
它应该让你朝着正确的方向前进。 ?read.zoo应该有助于理解它的作用。