以下是data.frames列表中的示例变量:
EUR.CLOSE
2014-08-28 1.3182
2014-08-29 1.3132
2014-09-01 1.3128
2014-09-02 1.3133
2014-09-03 1.3150
2014-09-04 1.2944
2014-09-05 1.2951
2014-09-08 1.2895
2014-09-09 1.2937
2014-09-10 1.2917
我有基于某些信号计算的行间隔。在这种情况下,假设计算的行间隔为rowintv <- c(5,10,17,22)
。然后我使用以下函数在这些间隔内获得最大值:
max.value <- sapply(seq_along(rowintv), function(x) max(data[findInterval(seq_along(data), rowintv, left.open = TRUE) == (x - 1)]))
这会返回[1] 1.3182 1.2951 1.2963 1.2849
但是,我想使用rep
在正确的子区间内将这些最大值附加到我的收盘价中,换句话说我想要一个输出:
EUR.CLOSE max.val
2014-08-28 1.3182 1.3182 (interval 1:5)
2014-08-29 1.3132 1.3182
2014-09-01 1.3128 1.3182
2014-09-02 1.3133 1.3182
2014-09-03 1.3150 1.3182
2014-09-04 1.2944 1.2951 (interval 6:10)
2014-09-05 1.2951 1.2951
2014-09-08 1.2895 1.2951
2014-09-09 1.2937 1.2951
2014-09-10 1.2917 1.2951
编辑:我正在考虑实施这个:
我想在data.frames列表中引入0,然后在间隔之间取1个周期滞后差异,并按差异复制最大值。对于上面的玩具数据,这适用于:
> row.intv <- c(0, rowintv)
> diff(eur.intv)
> [1] 5, 5, 7, 5
> sapply(1:length(eur.max), function(x) rep(eur.max[x], each=iterated[x]))
但是,当我尝试将其转换为data.frames列表时,我遇到了麻烦:
retrieve.atr <- function(x){
#Gets intervals
sig.intv <- which(newtrendsig[[x]][,3] != lag(newtrendsig[[x]][,3]))
#Gets max
max.val <- sapply(seq_along(sig.intv), function(y) max(newtrendsig[[x]][findInterval(seq_along(newtrendsig[[x]][,4]), sig.intv, left.open = TRUE) == (y - 1),4]))
#Adds 0 to the beginning to row index corresponding to signal interval
sig.intv <- sapply(1:length(sig.intv), function(x) sig.intv[[x]] <- c(0, sig.intv[[x]]))
#Takes difference to get number of replicates
iterate <- sapply(1:length(sig.intv), function(x) diff(sig.intv[[x]]))
#Replicates maximum values
max.vals <- sapply(1:length(max.val), function(x) rep(max.val[[x]], each=iterate[[x]]))
return(max.vals)
}
我收到错误Error in sig.intv[[x]] <- c(0, sig.intv[[x]]) :
more elements supplied than there are to replace
。
非常感谢帮助!下面附有示例输入:
structure(list(EUR.20D = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1), EUR.CLOSE = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1), EUR.20D.1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
EUR.CLOSE.1 = c(1.0683, 1.0698, 1.0655, 1.0643, 1.0598, 1.0578,
1.0601, 1.0674, 1.0616, 1.0614, 1.0536, 1.0558, 1.0582)), .Names = c("EUR.20D",
"EUR.CLOSE", "EUR.20D.1", "EUR.CLOSE.1"), row.names = c("2017-02-07",
"2017-02-08", "2017-02-09", "2017-02-10", "2017-02-13", "2017-02-14",
"2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", "2017-02-21",
"2017-02-22", "2017-02-23"), class = "data.frame")
structure(list(JPY.20D = c(-1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1), JPY.CLOSE = c(-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1), JPY.20D.1 = c(-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1), JPY.CLOSE.1 = c(112.39, 111.93, 113.25,
113.22, 113.74, 114.26, 114.16, 113.24, 112.84, 113.1, 113.68,
113.31, 112.61)), .Names = c("JPY.20D", "JPY.CLOSE", "JPY.20D.1",
"JPY.CLOSE.1"), row.names = c("2017-02-07", "2017-02-08", "2017-02-09",
"2017-02-10", "2017-02-13", "2017-02-14", "2017-02-15", "2017-02-16",
"2017-02-17", "2017-02-20", "2017-02-21", "2017-02-22", "2017-02-23"
), class = "data.frame")
structure(list(GBP.20D = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1), GBP.CLOSE = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1), GBP.20D.1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
GBP.CLOSE.1 = c(1.2509, 1.2541, 1.2497, 1.2491, 1.2526, 1.2468,
1.2461, 1.2489, 1.2412, 1.2463, 1.2473, 1.245, 1.2556)), .Names = c("GBP.20D",
"GBP.CLOSE", "GBP.20D.1", "GBP.CLOSE.1"), row.names = c("2017-02-07",
"2017-02-08", "2017-02-09", "2017-02-10", "2017-02-13", "2017-02-14",
"2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", "2017-02-21",
"2017-02-22", "2017-02-23"), class = "data.frame")
structure(list(CHF.CLOSE = c(-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1), CHF.OPEN = c(-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1), CHF.CLOSE.1 = c(-1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1), CHF.OPEN.1 = c(0.9978, 0.9947,
1.0017, 1.0024, 1.0057, 1.0062, 1.0055, 0.9971, 1.0026, 1.0028,
1.0097, 1.0103, 1.0063)), .Names = c("CHF.CLOSE", "CHF.OPEN",
"CHF.CLOSE.1", "CHF.OPEN.1"), row.names = c("2017-02-07", "2017-02-08",
"2017-02-09", "2017-02-10", "2017-02-13", "2017-02-14", "2017-02-15",
"2017-02-16", "2017-02-17", "2017-02-20", "2017-02-21", "2017-02-22",
"2017-02-23"), class = "data.frame")
structure(list(AUD.20D = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1), AUD.CLOSE = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1), AUD.20D.1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
AUD.CLOSE.1 = c(0.7628, 0.7645, 0.7625, 0.7674, 0.764, 0.7663,
0.771, 0.7694, 0.7664, 0.7688, 0.7675, 0.7703, 0.7715)), .Names = c("AUD.20D",
"AUD.CLOSE", "AUD.20D.1", "AUD.CLOSE.1"), row.names = c("2017-02-07",
"2017-02-08", "2017-02-09", "2017-02-10", "2017-02-13", "2017-02-14",
"2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", "2017-02-21",
"2017-02-22", "2017-02-23"), class = "data.frame")
structure(list(CAD.20D = c(-1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1), CAD.CLOSE = c(-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1), CAD.20D.1 = c(-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1), CAD.CLOSE.1 = c(1.3185, 1.3144, 1.3145,
1.3083, 1.3071, 1.3076, 1.308, 1.3071, 1.3096, 1.3106, 1.3141,
1.3164, 1.3105)), .Names = c("CAD.20D", "CAD.CLOSE", "CAD.20D.1",
"CAD.CLOSE.1"), row.names = c("2017-02-07", "2017-02-08", "2017-02-09",
"2017-02-10", "2017-02-13", "2017-02-14", "2017-02-15", "2017-02-16",
"2017-02-17", "2017-02-20", "2017-02-21", "2017-02-22", "2017-02-23"
), class = "data.frame")
structure(list(NZD.20D = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1), NZD.CLOSE = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1), NZD.20D.1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
NZD.CLOSE.1 = c(0.7301, 0.7266, 0.7187, 0.719, 0.7175, 0.7169,
0.7223, 0.721, 0.7182, 0.719, 0.7161, 0.7189, 0.723)), .Names = c("NZD.20D",
"NZD.CLOSE", "NZD.20D.1", "NZD.CLOSE.1"), row.names = c("2017-02-07",
"2017-02-08", "2017-02-09", "2017-02-10", "2017-02-13", "2017-02-14",
"2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", "2017-02-21",
"2017-02-22", "2017-02-23"), class = "data.frame")
structure(list(SEK.20D = c(-1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1), SEK.OPEN = c(-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1), SEK.20D.1 = c(-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1), SEK.OPEN.1 = c(8.8706, 8.8359, 8.9044,
8.919, 8.9397, 8.9333, 8.9203, 8.8601, 8.9119, 8.936, 8.9768,
8.9745, 8.9924)), .Names = c("SEK.20D", "SEK.OPEN", "SEK.20D.1",
"SEK.OPEN.1"), row.names = c("2017-02-07", "2017-02-08", "2017-02-09",
"2017-02-10", "2017-02-13", "2017-02-14", "2017-02-15", "2017-02-16",
"2017-02-17", "2017-02-20", "2017-02-21", "2017-02-22", "2017-02-23"
), class = "data.frame")
structure(list(NOK.20D = c(-1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1), NOK.CLOSE = c(-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1), NOK.20D.1 = c(-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1), NOK.CLOSE.1 = c(8.3213, 8.316, 8.3383,
8.3653, 8.3907, 8.3986, 8.3386, 8.2873, 8.3426, 8.3387, 8.3593,
8.3663, 8.3392)), .Names = c("NOK.20D", "NOK.CLOSE", "NOK.20D.1",
"NOK.CLOSE.1"), row.names = c("2017-02-07", "2017-02-08", "2017-02-09",
"2017-02-10", "2017-02-13", "2017-02-14", "2017-02-15", "2017-02-16",
"2017-02-17", "2017-02-20", "2017-02-21", "2017-02-22", "2017-02-23"
), class = "data.frame")
structure(list(CZK.20D = c(-1, 1, -1, -1, -1, -1, 1, 1, -1, 1,
-1, 1, 1), CZK.CLOSE = c(-1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1), CZK.20D.1 = c(-1, 0, -1, -1, -1, -1, 0, 0, -1,
0, -1, 0, 0), CZK.CLOSE.1 = c(25.2935, 25.2588, 25.3617, 25.3984,
25.495, 25.5467, 25.4905, 25.3155, 25.4578, 25.4625, 25.644,
25.5935, 25.536)), .Names = c("CZK.20D", "CZK.CLOSE", "CZK.20D.1",
"CZK.CLOSE.1"), row.names = c("2017-02-07", "2017-02-08", "2017-02-09",
"2017-02-10", "2017-02-13", "2017-02-14", "2017-02-15", "2017-02-16",
"2017-02-17", "2017-02-20", "2017-02-21", "2017-02-22", "2017-02-23"
), class = "data.frame")
structure(list(HUF.20D = c(-1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1), HUF.LOW.1 = c(-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1), HUF.20D.1 = c(-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1), HUF.LOW.1.1 = c(289.29, 288.72, 289.32,
289.71, 290.91, 291.18, 290.71, 288.08, 290.52, 290.2, 292.04,
291.65, 291.86)), .Names = c("HUF.20D", "HUF.LOW.1", "HUF.20D.1",
"HUF.LOW.1.1"), row.names = c("2017-02-07", "2017-02-08", "2017-02-09",
"2017-02-10", "2017-02-13", "2017-02-14", "2017-02-15", "2017-02-16",
"2017-02-17", "2017-02-20", "2017-02-21", "2017-02-22", "2017-02-23"
), class = "data.frame")
structure(list(ILS.20D = c(-1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1), ILS.LOW.1 = c(-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1), ILS.20D.1 = c(-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1), ILS.LOW.1.1 = c(3.7465, 3.7473, 3.7463,
3.7498, 3.7534, 3.7474, 3.7363, 3.7173, 3.7027, 3.7117, 3.7011,
3.699, 3.7024)), .Names = c("ILS.20D", "ILS.LOW.1", "ILS.20D.1",
"ILS.LOW.1.1"), row.names = c("2017-02-07", "2017-02-08", "2017-02-09",
"2017-02-10", "2017-02-13", "2017-02-14", "2017-02-15", "2017-02-16",
"2017-02-17", "2017-02-20", "2017-02-21", "2017-02-22", "2017-02-23"
), class = "data.frame")
structure(list(PLN.20D = c(-1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1), PLN.LOW.1 = c(-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1), PLN.20D.1 = c(-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1), PLN.LOW.1.1 = c(4.0316, 4.0288, 4.0473,
4.0362, 4.0668, 4.0685, 4.0665, 4.0466, 4.0844, 4.0695, 4.0771,
4.0715, 4.0654)), .Names = c("PLN.20D", "PLN.LOW.1", "PLN.20D.1",
"PLN.LOW.1.1"), row.names = c("2017-02-07", "2017-02-08", "2017-02-09",
"2017-02-10", "2017-02-13", "2017-02-14", "2017-02-15", "2017-02-16",
"2017-02-17", "2017-02-20", "2017-02-21", "2017-02-22", "2017-02-23"
), class = "data.frame")
structure(list(RUB.20D = c(-1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1), RUB.LOW.1 = c(-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1), RUB.20D.1 = c(-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1), RUB.LOW.1.1 = c(58.9062, 59.3441, 59.2339,
58.9703, 58.2567, 58.0109, 57.1273, 57.2545, 57.5934, 58.4497,
57.9695, 57.4262, 58.0724)), .Names = c("RUB.20D", "RUB.LOW.1",
"RUB.20D.1", "RUB.LOW.1.1"), row.names = c("2017-02-06", "2017-02-07",
"2017-02-08", "2017-02-09", "2017-02-10", "2017-02-13", "2017-02-14",
"2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", "2017-02-21",
"2017-02-22"), class = "data.frame")
structure(list(TRY.20D = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1), TRY.HIGH.1 = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1), TRY.20D.1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
TRY.HIGH.1.1 = c(3.7455, 3.719, 3.6822, 3.6976, 3.6723, 3.6515,
3.6599, 3.6674, 3.6309, 3.6245, 3.6204, 3.5799, 3.5689)), .Names = c("TRY.20D",
"TRY.HIGH.1", "TRY.20D.1", "TRY.HIGH.1.1"), row.names = c("2017-02-07",
"2017-02-08", "2017-02-09", "2017-02-10", "2017-02-13", "2017-02-14",
"2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", "2017-02-21",
"2017-02-22", "2017-02-23"), class = "data.frame")
structure(list(ZAR.20D = c(-1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1), ZAR.LOW.1 = c(-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1), ZAR.20D.1 = c(-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1), ZAR.LOW.1.1 = c(13.4502, 13.4139, 13.4035,
13.3365, 13.3349, 13.1053, 12.9162, 13.0327, 13.0382, 13.045,
13.1388, 12.9735, 12.872)), .Names = c("ZAR.20D", "ZAR.LOW.1",
"ZAR.20D.1", "ZAR.LOW.1.1"), row.names = c("2017-02-07", "2017-02-08",
"2017-02-09", "2017-02-10", "2017-02-13", "2017-02-14", "2017-02-15",
"2017-02-16", "2017-02-17", "2017-02-20", "2017-02-21", "2017-02-22",
"2017-02-23"), class = "data.frame")
structure(list(BRL.20D = c(-1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1), BRL.LOW.1 = c(-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1), BRL.20D.1 = c(-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1), BRL.LOW.1.1 = c(3.1203, 3.1156, 3.127,
3.1154, 3.1104, 3.0846, 3.0569, 3.0893, 3.0991, 3.0879, 3.0965,
3.0655, 3.0636)), .Names = c("BRL.20D", "BRL.LOW.1", "BRL.20D.1",
"BRL.LOW.1.1"), row.names = c("2017-02-07", "2017-02-08", "2017-02-09",
"2017-02-10", "2017-02-13", "2017-02-14", "2017-02-15", "2017-02-16",
"2017-02-17", "2017-02-20", "2017-02-21", "2017-02-22", "2017-02-23"
), class = "data.frame")
structure(list(CLP.20D = c(-1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1), CLP.LOW.1 = c(-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1), CLP.20D.1 = c(-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1), CLP.LOW.1.1 = c(647.45, 648.03, 646.27,
639.88, 642.81, 641.7, 639.11, 638.8, 644.79, 641.18, 643.45,
642.11, 641.43)), .Names = c("CLP.20D", "CLP.LOW.1", "CLP.20D.1",
"CLP.LOW.1.1"), row.names = c("2017-02-07", "2017-02-08", "2017-02-09",
"2017-02-10", "2017-02-13", "2017-02-14", "2017-02-15", "2017-02-16",
"2017-02-17", "2017-02-20", "2017-02-21", "2017-02-22", "2017-02-23"
), class = "data.frame")
structure(list(COP.20D = c(-1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1), COP.CLOSE = c(-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1), COP.20D.1 = c(-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1), COP.CLOSE.1 = c(2856.27, 2880.69, 2860.81,
2855.17, 2874.14, 2875.81, 2870, 2875.88, 2891.56, 2883.85, 2901.43,
2895.12, 2869.02)), .Names = c("COP.20D", "COP.CLOSE", "COP.20D.1",
"COP.CLOSE.1"), row.names = c("2017-02-07", "2017-02-08", "2017-02-09",
"2017-02-10", "2017-02-13", "2017-02-14", "2017-02-15", "2017-02-16",
"2017-02-17", "2017-02-20", "2017-02-21", "2017-02-22", "2017-02-23"
), class = "data.frame")
structure(list(MXN.20D = c(1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
-1, -1, -1), MXN.LOW.1 = c(-1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1), MXN.20D.1 = c(0, 0, 0, 0, 0, -1, -1, -1, -1,
-1, -1, -1, -1), MXN.LOW.1.1 = c(20.6208, 20.4793, 20.3504, 20.3461,
20.2823, 20.2605, 20.2748, 20.3802, 20.4292, 20.3718, 20.0138,
19.9067, 19.6669)), .Names = c("MXN.20D", "MXN.LOW.1", "MXN.20D.1",
"MXN.LOW.1.1"), row.names = c("2017-02-07", "2017-02-08", "2017-02-09",
"2017-02-10", "2017-02-13", "2017-02-14", "2017-02-15", "2017-02-16",
"2017-02-17", "2017-02-20", "2017-02-21", "2017-02-22", "2017-02-23"
), class = "data.frame")
structure(list(PEN.20D = c(-1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1), PEN.HIGH.1 = c(-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1), PEN.20D.1 = c(-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1), PEN.HIGH.1.1 = c(3.296, 3.2855, 3.268,
3.254, 3.261, 3.2582, 3.2457, 3.249, 3.264, 3.247, 3.247, 3.2455,
3.245)), .Names = c("PEN.20D", "PEN.HIGH.1", "PEN.20D.1", "PEN.HIGH.1.1"
), row.names = c("2017-02-07", "2017-02-08", "2017-02-09", "2017-02-10",
"2017-02-13", "2017-02-14", "2017-02-15", "2017-02-16", "2017-02-17",
"2017-02-20", "2017-02-21", "2017-02-22", "2017-02-23"), class = "data.frame")
structure(list(CNY.20D = c(-1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1), CNY.CLOSE = c(-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1), CNY.20D.1 = c(-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1), CNY.CLOSE.1 = c(6.8853, 6.8638, 6.8692,
6.8785, 6.8802, 6.8675, 6.8711, 6.853, 6.8665, 6.8785, 6.884,
6.8778, 6.8661)), .Names = c("CNY.20D", "CNY.CLOSE", "CNY.20D.1",
"CNY.CLOSE.1"), row.names = c("2017-02-07", "2017-02-08", "2017-02-09",
"2017-02-10", "2017-02-13", "2017-02-14", "2017-02-15", "2017-02-16",
"2017-02-17", "2017-02-20", "2017-02-21", "2017-02-22", "2017-02-23"
), class = "data.frame")
structure(list(IDR.20D = c(-1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1), IDR.LOW.1 = c(-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1), IDR.20D.1 = c(-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1), IDR.LOW.1.1 = c(13320, 13329, 13327,
13297, 13316, 13326, 13331, 13320, 13333, 13354, 13373, 13367,
13351)), .Names = c("IDR.20D", "IDR.LOW.1", "IDR.20D.1", "IDR.LOW.1.1"
), row.names = c("2017-02-06", "2017-02-07", "2017-02-08", "2017-02-09",
"2017-02-10", "2017-02-13", "2017-02-14", "2017-02-16", "2017-02-17",
"2017-02-20", "2017-02-21", "2017-02-22", "2017-02-23"), class = "data.frame")
structure(list(INR.20D = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0,
1), INR.LOW.1 = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1), INR.20D.1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
INR.LOW.1.1 = c(68.016, 67.9854, 67.9643, 67.9326, 67.8965,
67.8664, 67.8398, 67.8136, 67.7874, 67.7708, 67.7585, 67.7499,
67.7409)), .Names = c("INR.20D", "INR.LOW.1", "INR.20D.1",
"INR.LOW.1.1"), row.names = c("2017-02-03", "2017-02-06", "2017-02-07",
"2017-02-08", "2017-02-09", "2017-02-10", "2017-02-13", "2017-02-14",
"2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", "2017-02-22"
), class = "data.frame")
structure(list(KRW.20D = c(-1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1), KRW.LOW.1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0), KRW.20D.1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
), KRW.LOW.1.1 = c(4.4268, 4.4275, 4.4402, 4.4425, 4.443, 4.449,
4.449, 4.4475, 4.4525, 4.4545, 4.458, 4.4542, 4.451)), .Names = c("KRW.20D",
"KRW.LOW.1", "KRW.20D.1", "KRW.LOW.1.1"), row.names = c("2017-02-07",
"2017-02-08", "2017-02-09", "2017-02-10", "2017-02-13", "2017-02-14",
"2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", "2017-02-21",
"2017-02-22", "2017-02-23"), class = "data.frame")
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
EUR.20D Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13
EUR.CLOSE Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13
EUR.20D.1 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13
EUR.CLOSE.1 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13
[,8] [,9] [,10] [,11] [,12] [,13] [,14]
EUR.20D Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13
EUR.CLOSE Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13
EUR.20D.1 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13
EUR.CLOSE.1 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13
[,15] [,16] [,17] [,18] [,19] [,20] [,21]
EUR.20D Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13
EUR.CLOSE Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13
EUR.20D.1 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13
EUR.CLOSE.1 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13 Numeric,13
[,22] [,23] [,24] [,25]
EUR.20D Numeric,13 Numeric,13 Numeric,13 Numeric,13
EUR.CLOSE Numeric,13 Numeric,13 Numeric,13 Numeric,13
EUR.20D.1 Numeric,13 Numeric,13 Numeric,13 Numeric,13
EUR.CLOSE.1 Numeric,13 Numeric,13 Numeric,13 Numeric,13
答案 0 :(得分:0)
带有data.table和cut的解决方案:
library(data.table)
#Add 0 to your list of intervals
rowintv <- c(0,5,10,17,22)
#create column with row number
dat[,rowNum := 1:nrow(dat)]
#use cut to create column which has a unique value defined by vector of intervals
dat[,interval := cut(rowNum, breaks=rowintv)]
所以现在你有:
>dat
date close rowNum interval
1: 2014-08-28 1.3182 1 (0,5]
2: 2014-08-29 1.3132 2 (0,5]
3: 2014-09-01 1.3128 3 (0,5]
4: 2014-09-02 1.3133 4 (0,5]
5: 2014-09-03 1.3150 5 (0,5]
6: 2014-09-04 1.2944 6 (5,10]
7: 2014-09-05 1.2951 7 (5,10]
8: 2014-09-08 1.2895 8 (5,10]
9: 2014-09-09 1.2937 9 (5,10]
10: 2014-09-10 1.2917 10 (5,10]
然后......
#Find maximum value for each interval:
maxes.dt <- dat[, list(maxInInterval=max(close)), by=interval]
#Key each by interval and merge
setkey(maxes.dt, interval)
setkey(dat, interval)
dat.merged <- merge(dat, maxes.dt)
dat.merged
interval date close rowNum maxInInterval
1: (0,5] 2014-08-28 1.3182 1 1.3182
2: (0,5] 2014-08-29 1.3132 2 1.3182
3: (0,5] 2014-09-01 1.3128 3 1.3182
4: (0,5] 2014-09-02 1.3133 4 1.3182
5: (0,5] 2014-09-03 1.3150 5 1.3182
6: (5,10] 2014-09-04 1.2944 6 1.2951
7: (5,10] 2014-09-05 1.2951 7 1.2951
8: (5,10] 2014-09-08 1.2895 8 1.2951
9: (5,10] 2014-09-09 1.2937 9 1.2951
0: (5,10] 2014-09-10 1.2917 10 1.2951