R语言 每天获取时间范围 (xts) 中的最大值



XTS 对象 XTS1 以 15 分钟的间隔提供大约三天的数据。我想找到每天上午 900 点到上午 930 点之间注册的最大值,然后将这些值记录在向量上。如示例中所示。

然后我会在最低限度上做同样的事情。目标是记录金融工具的每日开盘范围(每日开盘范围=某个时间范围内的每日最大值和最小值)。为了简单起见,我忽略了此示例中的最小值。

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

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

你非常接近这行代码:

Max900.930am <- apply.daily(XTS1["T09:00/T09:30"], function(x) max(XTS1))

但是有一个关键的错别字:max(XTS1)应该是max(x)的。此错误是为什么您会获得整个系列的最大值,而不仅仅是一天。 这将起作用:

Max900.930am <- apply.daily(XTS1["T09:00/T09:30"], max)

您可以通过一次调用轻松获得最大值和最小值:

range0900.0930 <- apply.daily(XTS1["T09:00/T09:30"], range)

解决方案:

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。

最新更新