获取 X 行的最高值
Get highest value of X rows
我正在寻找一个函数来计算 XTS 对象上前 X 个周期 的最大值。该函数将 return 具有此类值的向量。
我相信有多种计算方法。令人惊讶的是,我找不到之前的 SO 问题中涵盖的内容。我希望有一个已经为此定义了功能的包。如果有 none 也许有人知道如何解决它。
下面的示例显示了 XTS 对象 XTS1 的最后 3 个周期中具有最高值的矢量。
library('xts')
XTS1 <- structure(c(12, 7, 7, 22, 24, 30, 26, 23, 27, 30), .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), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(10L, 1L))
#DESIRED OUTPUT
[,1] GetHighest(3)
2014-10-22 08:45:00 12 NA
2014-10-22 09:00:00 7 12
2014-10-22 09:15:00 7 12
2014-10-22 09:30:00 22 12
2014-10-22 09:45:00 24 22
2014-10-22 10:00:00 30 24
2014-10-22 10:15:00 26 30
2014-10-22 10:30:00 23 30
2014-10-22 10:45:00 27 30
2014-10-22 11:00:00 30 27
你可以使用 rollapply
from zoo.
所以它看起来像这样:
GetHighest_3 = rollapply(data = XTS1, width = 3, FUN = max)
然后合并:
cbind(XTS1, GetHighest_3)
我看到的唯一问题是前 2 个值可能 return 不适用,而不仅仅是第一个值,因为它的宽度为 3。
我无法测试代码,因为我现在无法访问 R,所以可能存在一些拼写错误。
我正在寻找一个函数来计算 XTS 对象上前 X 个周期 的最大值。该函数将 return 具有此类值的向量。
我相信有多种计算方法。令人惊讶的是,我找不到之前的 SO 问题中涵盖的内容。我希望有一个已经为此定义了功能的包。如果有 none 也许有人知道如何解决它。
下面的示例显示了 XTS 对象 XTS1 的最后 3 个周期中具有最高值的矢量。
library('xts')
XTS1 <- structure(c(12, 7, 7, 22, 24, 30, 26, 23, 27, 30), .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), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(10L, 1L))
#DESIRED OUTPUT
[,1] GetHighest(3)
2014-10-22 08:45:00 12 NA
2014-10-22 09:00:00 7 12
2014-10-22 09:15:00 7 12
2014-10-22 09:30:00 22 12
2014-10-22 09:45:00 24 22
2014-10-22 10:00:00 30 24
2014-10-22 10:15:00 26 30
2014-10-22 10:30:00 23 30
2014-10-22 10:45:00 27 30
2014-10-22 11:00:00 30 27
你可以使用 rollapply
from zoo.
所以它看起来像这样:
GetHighest_3 = rollapply(data = XTS1, width = 3, FUN = max)
然后合并:
cbind(XTS1, GetHighest_3)
我看到的唯一问题是前 2 个值可能 return 不适用,而不仅仅是第一个值,因为它的宽度为 3。
我无法测试代码,因为我现在无法访问 R,所以可能存在一些拼写错误。