使用 xts 库将毫秒时间序列聚合到秒
Aggregating millisecond time series to a second using xts library
我需要从 100 毫秒时间序列聚合 1 秒时间序列。我在 R xts 中使用函数 to.period()
,首先将数据转换为 xts 格式。
错误是:不支持的类型。
x <- xts(data, order.by = as.POSIXct(data$Time, format = "%H:%M:%S.%ms"))
to.period(x, period = "seconds", k=1)
Error in to.period(x, period = "seconds", k = 1) : unsupported type
当我尝试 period = minutes 和更长时间时出现同样的错误。
Date Time Price Amount
<NA> "2014/10/26" "17:30:12.500" "1.268" "1000000"
<NA> "2014/10/26" "17:33:28.900" "1.268" "1000000"
<NA> "2014/10/26" "17:33:52.600" "1.268" "1000000"
Mac OS
这个错误是因为我的数据是毫秒格式还是有其他原因?我该怎么做才能修复错误?
您的代码存在几个问题。我建议您花几分钟阅读 zoo 和 xts 的小插图,以更好地理解这两个 类.
假设您的 data
看起来像:
data <- structure(list(Date = c("2014/10/26", "2014/10/26", "2014/10/26"),
Time = c("17:30:12.500", "17:33:28.900", "17:33:52.600"),
Price = c(1.268, 1.268, 1.268), Amount = c(1000000L, 1000000L, 1000000L)),
.Names = c("Date", "Time", "Price", "Amount"), class = "data.frame",
row.names = c(NA, -3L))
您对 xts
构造函数的调用将不起作用。
x <- xts(data, order.by = as.POSIXct(data$Time, format = "%H:%M:%S.%ms"))
data$Time
只是一个时间,不是日期时间,所以 as.POSIXct
将使用今天的日期。但这不起作用,因为您指定的格式不正确("%ms"
不是有效格式,请参阅 ?strptime
),这会导致所有结果值为 NA
.
即使更正了这两个问题,您的代码仍然无法运行,因为 data
包含多种类型的数据(字符(因子?)、双精度、整数)并且 xts 对象是具有索引属性的矩阵,并且您不能在矩阵中混合类型。因此,您需要从 xts 对象的核心数据中排除日期和时间列。并且您需要正确指定 as.POSIXct
的 format
参数。
x <- xts(data[,c("Price", "Amount")],
order.by=as.POSIXct(paste(data$Date, data$Time), format="%Y/%m/%d %H:%M:%OS"))
现在 to.period
可以工作了:
to.period(x, "seconds")
# x.Open x.High x.Low x.Close
# 2014-10-26 17:30:12 1.268 1.268 1.268 1.268
# 2014-10-26 17:33:28 1.268 1.268 1.268 1.268
# 2014-10-26 17:33:52 1.268 1.268 1.268 1.268
如果要将 Amount 列聚合为 volume,您需要在调用 to.period
之前重命名它。
colnames(x)[2] <- "Volume"
to.period(x, "seconds")
# x.Open x.High x.Low x.Close x.Volume
# 2014-10-26 17:30:12 1.268 1.268 1.268 1.268 1e+06
# 2014-10-26 17:33:28 1.268 1.268 1.268 1.268 1e+06
# 2014-10-26 17:33:52 1.268 1.268 1.268 1.268 1e+06
我需要从 100 毫秒时间序列聚合 1 秒时间序列。我在 R xts 中使用函数 to.period()
,首先将数据转换为 xts 格式。
错误是:不支持的类型。
x <- xts(data, order.by = as.POSIXct(data$Time, format = "%H:%M:%S.%ms"))
to.period(x, period = "seconds", k=1)
Error in to.period(x, period = "seconds", k = 1) : unsupported type
当我尝试 period = minutes 和更长时间时出现同样的错误。
Date Time Price Amount
<NA> "2014/10/26" "17:30:12.500" "1.268" "1000000"
<NA> "2014/10/26" "17:33:28.900" "1.268" "1000000"
<NA> "2014/10/26" "17:33:52.600" "1.268" "1000000"
Mac OS
这个错误是因为我的数据是毫秒格式还是有其他原因?我该怎么做才能修复错误?
您的代码存在几个问题。我建议您花几分钟阅读 zoo 和 xts 的小插图,以更好地理解这两个 类.
假设您的 data
看起来像:
data <- structure(list(Date = c("2014/10/26", "2014/10/26", "2014/10/26"),
Time = c("17:30:12.500", "17:33:28.900", "17:33:52.600"),
Price = c(1.268, 1.268, 1.268), Amount = c(1000000L, 1000000L, 1000000L)),
.Names = c("Date", "Time", "Price", "Amount"), class = "data.frame",
row.names = c(NA, -3L))
您对 xts
构造函数的调用将不起作用。
x <- xts(data, order.by = as.POSIXct(data$Time, format = "%H:%M:%S.%ms"))
data$Time
只是一个时间,不是日期时间,所以 as.POSIXct
将使用今天的日期。但这不起作用,因为您指定的格式不正确("%ms"
不是有效格式,请参阅 ?strptime
),这会导致所有结果值为 NA
.
即使更正了这两个问题,您的代码仍然无法运行,因为 data
包含多种类型的数据(字符(因子?)、双精度、整数)并且 xts 对象是具有索引属性的矩阵,并且您不能在矩阵中混合类型。因此,您需要从 xts 对象的核心数据中排除日期和时间列。并且您需要正确指定 as.POSIXct
的 format
参数。
x <- xts(data[,c("Price", "Amount")],
order.by=as.POSIXct(paste(data$Date, data$Time), format="%Y/%m/%d %H:%M:%OS"))
现在 to.period
可以工作了:
to.period(x, "seconds")
# x.Open x.High x.Low x.Close
# 2014-10-26 17:30:12 1.268 1.268 1.268 1.268
# 2014-10-26 17:33:28 1.268 1.268 1.268 1.268
# 2014-10-26 17:33:52 1.268 1.268 1.268 1.268
如果要将 Amount 列聚合为 volume,您需要在调用 to.period
之前重命名它。
colnames(x)[2] <- "Volume"
to.period(x, "seconds")
# x.Open x.High x.Low x.Close x.Volume
# 2014-10-26 17:30:12 1.268 1.268 1.268 1.268 1e+06
# 2014-10-26 17:33:28 1.268 1.268 1.268 1.268 1e+06
# 2014-10-26 17:33:52 1.268 1.268 1.268 1.268 1e+06