3

我有一个包含日内数据的 xts 对象:

head(stocks[,1])
                    SMH.close
2009-01-02 09:31:00     17.66
2009-01-02 09:32:00     17.66
2009-01-02 09:33:00     17.64
2009-01-02 09:34:00     17.60
2009-01-02 09:35:00     17.58
2009-01-02 09:36:00     17.63

我想对日内数据进行各种分析,但操作不应跨越日间界限。所以我想做的是按日期拆分数据(忽略时间)。因此,我通过以下方式提取了索引并保存了唯一的日期值:

y <- index(stocks)
x <- strptime(y, format="%Y-%m-%d")
uniquedates <- unique(x)

现在我想做一些类似于 ?split 中的示例的事情

> g <- airquality$Month
> l <- split(airquality, g)

在这里,空气质量数据按 Month 列的值拆分为一个列表对象。我不确定如何做类似的事情,因为日期在我的情况下是索引而不是数据列。我试过但得到一个错误。

> split(stocks, uniquedates)
Error in args[[i]] : subscript out of bounds

也许有一种更清洁的方法可以实现我想做的事情。非常感谢您的帮助。

4

2 回答 2

3

您应该跳过 unique() 步骤。只需使用带有f="days"参数的 split.xts 即可。

data(sample_matrix)
sample.xts <- as.xts(sample_matrix, descr='my new xts object')
split.xts(sample.xts, f="days")
[[1]]
               Open     High      Low    Close
2007-01-02 50.03978 50.11778 49.95041 50.11778

[[2]]
              Open     High     Low    Close
2007-01-03 50.2305 50.42188 50.2305 50.39767

[[3]]
               Open     High      Low    Close
2007-01-04 50.42096 50.42096 50.26414 50.33236

[[4]]
snipped
于 2011-05-15T01:23:31.680 回答
0

也许您正在寻找apply.daily(或period.apply一般而言)?

于 2011-05-14T23:35:18.043 回答