首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >每天在时间范围内获得最高值(xts)

每天在时间范围内获得最高值(xts)
EN

Stack Overflow用户
提问于 2017-07-06 09:06:55
回答 2查看 1.1K关注 0票数 1

XTS对象XTS1每隔15分钟就有大约3天的数据。我希望找到每天早上900点到930点之间注册的最大值,然后在向量上记录这样的值。如本例所示。

然后,我会做同样的最低限度。目的是记录金融工具的日开盘价范围(日开盘价=某一时段内的日开盘价和最低开盘价)。为了保持简单,我忽略了这个例子中的最小值。

这个问题与this one非常相似,如果不是重复的话。然而,这里提供的解决方案是在具有日频率的数据库中找到每周最大值,而这个解决方案是在具有日内频率的数据库中找到每小时最大值。更重要的是,该解决方案在这种情况下不起作用(见下文)。

代码语言:javascript
运行
复制
library('xts')
XTS1 <- structure(c(0.2284, 0.2283, 0.2284, 0.2288, 0.2307, 0.2319, 0.2322, 0.2327, 0.2328, 0.2327, 0.2327, 0.2327, 0.2331, 0.2338, 0.2338, 0.2346, 0.2348, 0.2348, 0.236, 0.2361, 0.2362, 0.236, 0.2365, 0.2364, 0.2363, 0.236, 0.2359, 0.2364, 0.2365, 0.2363, 0.2363, 0.2362, 0.2363, 0.2364, 0.2365, 0.2365, 0.2364, 0.2364, 0.2363, 0.2363, 0.2364, 0.2363, 0.2363, 0.2363, 0.2364, 0.2364, 0.2364, 0.2364, 0.2365, 0.2365, 0.2365, 0.2365, 0.2365, 0.2366, 0.2366, 0.2365, 0.2365, 0.2364, 0.2364, 0.2364, 0.2364, 0.2363, 0.2365, 0.2365, 0.2366, 0.2365, 0.2365, 0.2364, 0.2364, 0.2368, 0.2373, 
                    0.2378, 0.2379, 0.2382, 0.2383, 0.2384, 0.2384, 0.2384, 0.2383, 0.2385, 0.2385, 0.2387, 0.2385, 0.2385, 0.2385, 0.2386, 0.2387, 0.2387, 0.24, 0.2394, 0.2396, 0.2394, 0.2398, 0.2394, 0.2396, 0.24, 0.24, 0.2401, 0.2397, 0.2399, 0.2492, 0.2398, 0.2203, 0.2104, 0.2104, 0.2404, 0.2398, 0.24, 0.2401, 0.2402, 0.2399, 0.24, 0.24, 0.2403, 0.2406, 0.2401, 0.2404, 0.2406, 0.2406, 0.2405, 0.241, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.241, 0.2405, 0.2406, 0.2405, 0.2404, 0.2404, 0.2406, 0.2408, 0.2406, 0.2406, 0.2407, 0.2407, 0.2407, 0.2408, 0.2408, 0.2408, 0.2408, 0.2408, 0.2408, 0.2408, 0.2411, 0.241, 0.241, 0.2411, 0.2411, 0.2412, 0.2412, 0.2411, 0.2411, 0.2411, 0.2412, 0.2412, 0.2412, 0.2412, 0.2412, 0.2412, 0.2412, 0.2411, 0.2412, 0.2412, 0.2412, 0.2413, 0.2412, 0.2411, 0.2411, 0.241, 0.241, 0.2411, 0.2409, 0.2417, 0.2422, 0.2421, 0.2428, 0.2426, 0.2428, 0.2421, 0.2421, 0.2419, 0.2422, 0.2424, 0.2427, 0.2437, 0.2438, 0.2441, 0.2442), .Dim = c(199L, 1L), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "", class = c("xts", "zoo"), .CLASS = structure("double", class = "CLASS"), formattable = structure(list(formatter = "formatC", format = structure(list(format = "f", digits = 2), .Names = c("format", "digits")), preproc = "percent_preproc", postproc = "percent_postproc"), .Names = c("formatter", "format", "preproc", "postproc")), index = structure(c(1413981900, 1413982800, 1413983700, 1413984600, 1413985500, 1413986400, 1413987300, 1413988200, 1413989100, 1413990000, 1413990900, 1413991800, 1413992700, 1413993600, 1413994500, 1413995400, 1413996300, 1413997200, 1413998100, 1413999000, 1413999900, 1414000800, 1414001700, 1414002600, 1414003500, 1414004400, 1414005300, 1414006200, 1414007100, 1414008000, 1414009800, 1414010700, 1414011600, 1414015200, 1414016100, 1414017000, 1414017900, 1414018800, 1414019700, 1414020600, 1414021500, 1414022400, 1414023300, 1414024200, 1414025100, 1414026000, 1414026900, 1414027800, 1414028700, 1414029600, 1414030500, 1414031400, 1414032300, 1414033200, 1414034100, 1414035000, 1414035900, 1414036800, 1414037700, 1414038600, 1414039500, 1414040400, 1414041300, 1414042200, 1414043100, 1414044000, 1414044900, 1414045800, 1414046700, 1414047600, 1414048500, 1414049400, 1414050300, 1414051200, 1414052100, 1414053000, 1414053900, 1414054800, 1414055700, 1414056600, 1414057500, 1414058400, 1414059300, 1414060200, 1414061100, 1414062000, 1414062900, 1414063800, 1414064700, 1414065600, 1414066500, 1414067400, 1414068300, 1414069200, 1414070100, 1414071000, 1414071900, 1414072800, 1414073700, 1414074600, 1414075500, 1414076400, 1414077300, 1414078200, 1414079100, 1414080000, 1414080900, 1414081800, 1414082700, 1414083600, 1414084500, 1414085400, 1414086300, 1414087200, 1414088100, 1414089000, 1414089900, 1414090800, 1414091700, 1414092600, 1414093500, 1414094400, 1414096200, 1414097100, 1414098000, 1414101600, 1414102500, 1414103400, 1414104300, 1414105200, 1414106100, 1414107000, 1414107900, 1414108800, 1414109700, 1414110600, 1414111500, 1414112400, 1414113300, 1414114200, 1414115100, 1414116000, 1414116900, 1414117800, 1414118700, 1414119600, 1414120500, 1414121400, 1414122300, 1414123200, 1414124100, 1414125000, 1414125900, 1414126800, 1414127700, 1414128600, 1414129500, 1414130400, 1414131300, 1414132200, 1414133100, 1414134000, 1414134900, 1414135800, 1414136700, 1414137600, 1414138500, 1414139400, 1414140300, 1414141200, 1414142100, 1414143000, 1414143900, 1414144800, 1414145700, 1414146600, 1414147500, 1414148400, 1414149300, 1414150200, 1414151100, 1414152000, 1414152900, 1414153800, 1414154700, 1414155600, 1414156500, 1414157400, 1414158300, 1414159200, 1414160100, 1414161000, 1414161900, 1414162800, 1414163700, 1414164600, 1414165500, 1414166400, 1414167300), tzone = "", tclass = c("POSIXct", "POSIXt")))

#DESIRED OUTPUT
                      [,1]  Max900.930am
2014-10-22 08:45:00 0.2284            NA
2014-10-22 09:00:00 0.2283        0.2283    
2014-10-22 09:15:00 0.2284        0.2284
2014-10-22 09:30:00 0.2288        0.2288
2014-10-22 09:45:00 0.2307        0.2288
2014-10-22 10:00:00 0.2319        0.2288
.....

#This records max on desired time range, but in whole database rather than for every day
max(XTS1['T09:00:00/T09:30:00'])

#This says "Error: width > 0 is not TRUE"
xxx = rollapply(data = XTS1, width = XTS1['T09:00:00/T09:30:00'], FUN = max)

#This is derived from solution by @JoshuaUlrich to the other question
#Doesn't work: records max on desired time range, in whole database rather than every day
 Max900.930am <- apply.daily(XTS1["T09:00/T09:30"], function(x) max(XTS1))
 y <- merge(XTS1, Max900.930am, fill = na.locf)

#Printout of Max900.930am, showing the problem 
                      [,1]
2014-10-22 09:30:00 0.2492
2014-10-23 09:30:00 0.2492
2014-10-24 09:30:00 0.2492
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-08 12:50:15

您非常接近这一行代码:

代码语言:javascript
运行
复制
Max900.930am <- apply.daily(XTS1["T09:00/T09:30"], function(x) max(XTS1))

但是有一个关键的错误:max(XTS1)应该是max(x)。这个错误是为什么你得到了整个系列的最大值,而不仅仅是一天。这样做是可行的:

代码语言:javascript
运行
复制
Max900.930am <- apply.daily(XTS1["T09:00/T09:30"], max)

只要一个电话,你就可以很容易地得到最大值和最小值:

代码语言:javascript
运行
复制
range0900.0930 <- apply.daily(XTS1["T09:00/T09:30"], range)
票数 1
EN

Stack Overflow用户

发布于 2017-07-06 13:33:53

解决方案:

代码语言:javascript
运行
复制
 library('data.table')
 Max900.930am <- apply.daily(XTS1, function(x) max(x['T09:00:00/T09:15:00']))
 y <- merge(XTS1, Max900.930am, fill = na.locf)
 y$Max900.930am <- shift(y$Max900.930am, n=53, type=c("lead"))

这要感谢@jbaums。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44944449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档