我有包含這塊數據對僅位於特定時間間隔內的值執行算術表達式?
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
的我具有被基於一定的信號來計算行間隔data.frames的列表。在這種情況下,假設計算的行間隔是rowintv <- c(5,10,17,22)
。我用下面的函數來獲取基於行間隔的最大收盤價:
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
。但是,現在我想在算術表達式中使用這些最大值,並使用與子區間相匹配的適當收盤價。舉例來說,我想:
EUR.CLOSE
2014-08-28 1.3182 - 3*1.3182
2014-08-29 1.3132 - 3* 1.3182
2014-09-01 1.3128 - 3* 1.3182
2014-09-02 1.3133 - 3* 1.3182
2014-09-03 1.3150 - 3* 1.3182 (this is 5th row)
2014-09-04 1.2944 - 3 * 1.2951 (for rows 6-10 we use maximum value 1.2951)
我試着
sapply(seq_along(sig.intv), function(y) newtrendsig[[x]][findInterval(seq_along(newtrendsig[[x]][,4]), sig.intv, left.open = TRUE) == (y * newtrendsig[[x]][,3] - max.val),4])
這樣做,但這種返回警告說In y * newtrendsig[[x]][, 3] - max.val : longer object length is not a multiple of shorter object length
附件是我的原始數據的樣本的dput
。收盤價格可以通過data[[x]][,4]
獲得。
structure(list(EUR.20D = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), EUR.CLOSE = c(-1,
-1, -1, -1, -1, -1, -1, -1, -1, -1), EUR.20D.1 = c(0, 0, 0, 0,
0, 0, 0, 0, 0, 0), EUR.CLOSE.1 = c(1.0643, 1.0598, 1.0578, 1.0601,
1.0674, 1.0616, 1.0614, 1.0536, 1.0558, 1.0582), EUR.20D.2 = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("MA crossover",
"Price break"), class = "factor"), atr...2. = c(0.00917523899027,
0.00921272191953643, 0.00911895606814097, 0.00893903063470233,
0.00997195701793786, 0.00953110294522802, 0.00904316702056887,
0.00858294080481395, 0.00842701646161295, 0.00826080100006917
), EUR.CLOSE.2 = c(-0.607022786701527, -1.02726933134104, -1.15866193234909,
-0.925233644859815, -0.252312867956279, -0.7479431563201, -0.701655907942751,
-1.33907669257421, -1.04039741306588, -0.768942235558888), EUR.CLOSE.3 = c(0.377251721210978,
-0.00943485234455977, -0.179296027177504, 0.0660751368699401,
0.745634733364796, 0.18875047187618, 0.179329872581408, -0.547479705493657,
-0.339814989616748, -0.141549495140139), EUR.CLOSE.4 = c(-0.0011268664916733,
-0.00423709498833686, -0.00188893142678216, 0.00217196364717242,
0.00686254162079234, -0.00544858088184653, -0.000188412624203348,
-0.00737591996472312, 0.00208590196044455, 0.00227057808051545
)), .Names = c("EUR.20D", "EUR.CLOSE", "EUR.20D.1", "EUR.CLOSE.1",
"EUR.20D.2", "atr...2.", "EUR.CLOSE.2", "EUR.CLOSE.3", "EUR.CLOSE.4"
), na.action = structure(1:19, .Names = c("2014-08-28", "2014-08-29",
"2014-09-01", "2014-09-02", "2014-09-03", "2014-09-04", "2014-09-05",
"2014-09-08", "2014-09-09", "2014-09-10", "2014-09-11", "2014-09-12",
"2014-09-15", "2014-09-16", "2014-09-17", "2014-09-18", "2014-09-19",
"2014-09-22", "2014-09-23"), class = "omit"), row.names = c("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), JPY.CLOSE = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1), JPY.20D.1 = c(-1,
-1, -1, -1, -1, -1, -1, -1, -1, -1), JPY.CLOSE.1 = c(113.22,
113.74, 114.26, 114.16, 113.24, 112.84, 113.1, 113.68, 113.31,
112.61), JPY.20D.2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("MA crossover", "Price break"), class = "factor"),
atr...2. = c(0.825994754458168, 0.804137986282584, 0.831699558690971,
0.845149590213044, 0.828353190912112, 0.809899391561248,
0.817049435021158, 0.779403046805361, 0.755874257747835,
0.727597525051561), JPY.CLOSE.2 = c(-0.16753372718455, 0.317516316810725,
0.705094306363486, 0.634696755994357, -0.105857445306991,
-0.379623907477702, -0.167711183687891, 0.353107344632773,
0.0176538088092471, -0.512412757310715), JPY.CLOSE.3 = c(-1.65899418049162,
-1.20733084339442, -0.755667506297221, -0.851137745353486,
-1.6330785267547, -1.93795081254887, -1.68636995827539, -1.15642118076689,
-1.40955364134691, -1.92475178540323), JPY.CLOSE.4 = c(-0.000264935754629825,
0.00458231327013969, 0.00456141141766331, -0.000875580127735809,
-0.00809151292488242, -0.00353857410990255, 0.00230149698827908,
0.00511510066677001, -0.00326005839636956, -0.00619690368354142
)), .Names = c("JPY.20D", "JPY.CLOSE", "JPY.20D.1", "JPY.CLOSE.1",
"JPY.20D.2", "atr...2.", "JPY.CLOSE.2", "JPY.CLOSE.3", "JPY.CLOSE.4"
), na.action = structure(1:19, .Names = c("2014-08-28", "2014-08-29",
"2014-09-01", "2014-09-02", "2014-09-03", "2014-09-04", "2014-09-05",
"2014-09-08", "2014-09-09", "2014-09-10", "2014-09-11", "2014-09-12",
"2014-09-15", "2014-09-16", "2014-09-17", "2014-09-18", "2014-09-19",
"2014-09-22", "2014-09-23"), class = "omit"), row.names = c("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), GBP.CLOSE = c(-1,
-1, -1, -1, -1, -1, -1, -1, -1, -1), GBP.20D.1 = c(0, 0, 0, 0,
0, 0, 0, 0, 0, 0), GBP.CLOSE.1 = c(1.2491, 1.2526, 1.2468, 1.2461,
1.2489, 1.2412, 1.2463, 1.2473, 1.245, 1.2556), GBP.20D.2 = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("MA crossover",
"Price break"), class = "factor"), atr...2. = c(0.0117578295696,
0.0116108417432, 0.0117386387615428, 0.0119501645642898, 0.0120680099525548,
0.0116131520988009, 0.0118336412346008, 0.0114026668607008, 0.0109524763706507,
0.0105058709156042), GBP.CLOSE.2 = c(0.120230843218967, 0.207999999999995,
-0.279932816124135, -0.41556780947815, -0.247603833865823, -0.878453921098858,
-0.439367311072061, -0.3435602428891, -0.455744782921549, 0.407836865253907
), GBP.CLOSE.3 = c(0.563561710007237, 0.877828783119909, 0.451176281018355,
0.419050689016044, 0.660917224147657, 0.0644953240890144, 0.50806451612903,
0.621168118747986, 0.451831531386159, 1.29073894804776), GBP.CLOSE.4 = c(-0.00048023051987453,
0.00279809911944895, -0.00464112219813048, -0.000561594944360089,
0.0022444899202175, -0.00618451029481418, 0.00410050825810596,
0.000802053299332334, -0.00184568523285542, 0.00847801606899046
)), .Names = c("GBP.20D", "GBP.CLOSE", "GBP.20D.1", "GBP.CLOSE.1",
"GBP.20D.2", "atr...2.", "GBP.CLOSE.2", "GBP.CLOSE.3", "GBP.CLOSE.4"
), na.action = structure(1:19, .Names = c("2014-08-28", "2014-08-29",
"2014-09-01", "2014-09-02", "2014-09-03", "2014-09-04", "2014-09-05",
"2014-09-08", "2014-09-09", "2014-09-10", "2014-09-11", "2014-09-12",
"2014-09-15", "2014-09-16", "2014-09-17", "2014-09-18", "2014-09-19",
"2014-09-22", "2014-09-23"), class = "omit"), row.names = c("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), CHF.OPEN = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1),
CHF.CLOSE.1 = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1),
CHF.OPEN.1 = c(1.0024, 1.0057, 1.0062, 1.0055, 0.9971, 1.0026,
1.0028, 1.0097, 1.0103, 1.0063), CHF.CLOSE.2 = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("MA crossover",
"Price break"), class = "factor"), atr...2. = c(0.00728057182205798,
0.00722481669191098, 0.00708732978534591, 0.00693109194353549,
0.00752887109042581, 0.00716966601253826, 0.00682897558307124,
0.00649119161285187, 0.0063489636405053, 0.00615260909475493
), CHF.OPEN.2 = c(0.360432519022821, 0.721081622433649, 0.75097626914989,
0.690967354296016, -0.110198357042676, 0.440793428170703,
0.430645968953416, 1.07107107107108, 1.08054027013506, 0.650130026005196
), CHF.OPEN.3 = c(-0.870253164556976, -0.543908227848107,
-0.484620710117706, -0.553852240134512, -1.34560205797961,
-0.771971496437058, -0.732528212235208, -0.049495149475346,
0.0297029702970264, -0.316988608221901), CHF.OPEN.4 = c(0.000698567964139939,
0.00328669186857626, 0.00049704260678339, -0.000695928844511353,
-0.00838914337795957, 0.00550083899465574, 0.000199461454735274,
0.00685716972613716, 0.000594059423411153, -0.00396707849439904
)), .Names = c("CHF.CLOSE", "CHF.OPEN", "CHF.CLOSE.1", "CHF.OPEN.1",
"CHF.CLOSE.2", "atr...2.", "CHF.OPEN.2", "CHF.OPEN.3", "CHF.OPEN.4"
), na.action = structure(1:19, .Names = c("2014-08-28", "2014-08-29",
"2014-09-01", "2014-09-02", "2014-09-03", "2014-09-04", "2014-09-05",
"2014-09-08", "2014-09-09", "2014-09-10", "2014-09-11", "2014-09-12",
"2014-09-15", "2014-09-16", "2014-09-17", "2014-09-18", "2014-09-19",
"2014-09-22", "2014-09-23"), class = "omit"), row.names = c("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), AUD.CLOSE = c(-1,
-1, -1, -1, -1, -1, -1, -1, -1, -1), AUD.20D.1 = c(0, 0, 0, 0,
0, 0, 0, 0, 0, 0), AUD.CLOSE.1 = c(0.7674, 0.764, 0.7663, 0.771,
0.7694, 0.7664, 0.7688, 0.7675, 0.7703, 0.7715), AUD.20D.2 = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("MA crossover",
"Price break"), class = "factor"), atr...2. = c(0.0100596741007665,
0.0100625545221403, 0.0100438006277017, 0.00981924344000875,
0.00956786890857955, 0.0095130211293953, 0.00932637676300992,
0.00889592127993779, 0.00850335547422795, 0.00819597294035453
), AUD.CLOSE.2 = c(1.12004216629331, 0.566012899828876, 0.802420415680083,
1.28744088281661, 0.984381152382196, 0.511475409836068, 0.760157273918745,
0.537070998166098, 0.811412118832611, 0.849673202614373), AUD.CLOSE.3 = c(3.21452589105581,
2.7019760720527, 2.95579739352412, 3.53162347253928, 3.24745034889962,
2.78969957081544, 3.05630026809652, 2.82690246516613, 3.1329495247021,
3.18309482412731), AUD.CLOSE.4 = c(0.00640566933128534, -0.00444038864633145,
0.00300594880983807, 0.0061146355869709, -0.00207738324513479,
-0.00390676366154397, 0.00312663099943206, -0.00169237819473061,
0.00364156989756792, 0.00155662244429539)), .Names = c("AUD.20D",
"AUD.CLOSE", "AUD.20D.1", "AUD.CLOSE.1", "AUD.20D.2", "atr...2.",
"AUD.CLOSE.2", "AUD.CLOSE.3", "AUD.CLOSE.4"), na.action = structure(1:19, .Names = c("2014-08-28",
"2014-08-29", "2014-09-01", "2014-09-02", "2014-09-03", "2014-09-04",
"2014-09-05", "2014-09-08", "2014-09-09", "2014-09-10", "2014-09-11",
"2014-09-12", "2014-09-15", "2014-09-16", "2014-09-17", "2014-09-18",
"2014-09-19", "2014-09-22", "2014-09-23"), class = "omit"), row.names = c("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), CAD.CLOSE = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1), CAD.20D.1 = c(-1,
-1, -1, -1, -1, -1, -1, -1, -1, -1), CAD.CLOSE.1 = c(1.3083,
1.3071, 1.3076, 1.308, 1.3071, 1.3096, 1.3106, 1.3141, 1.3164,
1.3105), CAD.20D.2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = "MA crossover", class = "factor"), atr...2. = c(0.00897996644549454,
0.00881711169938778, 0.0089516037208601, 0.00875506059794152,
0.00876541341237426, 0.00865359816863325, 0.00852119829944516,
0.00806254127805621, 0.00797235975819507, 0.007631476918324),
CAD.CLOSE.2 = c(-0.403471376370286, -0.456933973040896, -0.434021168049953,
-0.327668978129999, -0.30508733124857, -0.0305343511450348,
0.0992896967845474, 0.374274366025063, 0.511567534549908,
0.0534432737822509), CAD.CLOSE.3 = c(-1.24547101449275, -1.30625188764724,
-1.23867069486406, -1.18606935106142, -1.23913864752552,
-1.03529056147509, -0.952237001209186, -0.695231617924875,
-0.498866213151923, -0.914864660517163), CAD.CLOSE.4 = c(-0.00472778065310547,
-0.000917641725324381, 0.000382453058549392, 0.000305857167088586,
-0.000688310225637978, 0.00191080426541917, 0.000763300548471235,
0.00266697304419594, 0.00174871741959248, -0.00449199430572389
)), .Names = c("CAD.20D", "CAD.CLOSE", "CAD.20D.1", "CAD.CLOSE.1",
"CAD.20D.2", "atr...2.", "CAD.CLOSE.2", "CAD.CLOSE.3", "CAD.CLOSE.4"
), na.action = structure(1:19, .Names = c("2014-08-28", "2014-08-29",
"2014-09-01", "2014-09-02", "2014-09-03", "2014-09-04", "2014-09-05",
"2014-09-08", "2014-09-09", "2014-09-10", "2014-09-11", "2014-09-12",
"2014-09-15", "2014-09-16", "2014-09-17", "2014-09-18", "2014-09-19",
"2014-09-22", "2014-09-23"), class = "omit"), row.names = c("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")
謝謝。我嘗試使用'res < - sapply(seq_along(sig.intv),function(x)sig.intv < - which(newtrendsig [[x]] [,])爲我的原始數據(數據框列表) 3]!= lag(newtrendsig [[x]] [,3])) newtrendsig [[x]] [findInterval(seq_along(newtrendsig [[x]] [,4]),sig.intv,left.open = TRUE)==(x-1),4] - 3 * sig.intv [x] } )其中'newtrendsig [[x]] [,4]'對應於收盤價,但我得到一個下標的bounsd錯誤。我會在原始文章中附上我的原始數據樣本。 – Nikitau
@Nikitau我編輯了我的答案,你可能沒有收到通知 – minem