R从索引中搜索特定时间
我使用R从索引中搜索特定时间,r,xts,R,Xts,我使用xts对象。该对象的索引如下所示。一年中每天每小时都有一个 "2011-01-02 18:59:00 EST" "2011-01-02 19:58:00 EST" "2011-01-02 20:59:00 EST" 列中是与每个索引项关联的值。我想做的是计算全年所有周一18:59的值的标准偏差。今年应该有52个值 我可以使用weekdays()函数搜索一周中的某一天,但我的问题是搜索时间,例如18:59:00或任何其他时间。您可以使用.index*函数系列(不要忘记“index”前面的“
xts
对象。该对象的索引如下所示。一年中每天每小时都有一个
"2011-01-02 18:59:00 EST"
"2011-01-02 19:58:00 EST"
"2011-01-02 20:59:00 EST"
列中是与每个索引项关联的值。我想做的是计算全年所有周一18:59
的值的标准偏差。今年应该有52个值
我可以使用
weekdays()
函数搜索一周中的某一天,但我的问题是搜索时间,例如18:59:00
或任何其他时间。您可以使用.index*
函数系列(不要忘记“index”前面的“.”
用于划分您使用的时间
fxts["T19:30/T20:00"] # this will give you the time period you are looking for
这里你把工作日和时间段结合起来
fxts["T18:30/T20:00"] & fxts[.indexwday(fxts)==1] # to get a logical vector or
fxts["T18:30/T21:00"][.indexwday(fxts["T18:30/T21:00"])==1] # to get the values
> value
2011-01-03 18:58:00 3
2011-01-10 18:59:00 6
您可以使用
交互
从工作日
和索引小时
的组合中创建一个因子,然后使用拆分
从xts对象中选择相关观察值
set.seed(21)
x <- .xts(rnorm(1e4), seq(1, by=60*60, length.out=1e4))
groups <- interaction(weekdays(index(x)), .indexhour(x))
output <- lapply(split(x, groups), function(x) c(count=length(x), sd=sd(x)))
output <- do.call(rbind, output)
head(output)
# count sd
# Friday.0 60 1.0301030
# Monday.0 59 0.9204670
# Saturday.0 60 0.9842125
# Sunday.0 60 0.9500347
# Thursday.0 60 0.9506620
# Tuesday.0 59 0.8972697
set.seed(21)
x
set.seed(21)
x <- .xts(rnorm(1e4), seq(1, by=60*60, length.out=1e4))
groups <- interaction(weekdays(index(x)), .indexhour(x))
output <- lapply(split(x, groups), function(x) c(count=length(x), sd=sd(x)))
output <- do.call(rbind, output)
head(output)
# count sd
# Friday.0 60 1.0301030
# Monday.0 59 0.9204670
# Saturday.0 60 0.9842125
# Sunday.0 60 0.9500347
# Thursday.0 60 0.9506620
# Tuesday.0 59 0.8972697