我有一个具有日内数据的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)
此处,空气质量数据按月份列的值拆分为列表对象。我不确定如何做类似的事情,因为日期是我的情况下的索引,而不是数据列。我试过但得到了一个错误。
> split(stocks, uniquedates)
Error in args[[i]] : subscript out of bounds
也许有一种更清洁的方式来实现我想做的事情。我非常感谢你的帮助。
答案 0 :(得分:2)
您应该跳过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
答案 1 :(得分:0)
也许您正在寻找apply.daily
(或一般period.apply
)?