Подмножество моего исходного фрейма данных, DF, имеет следующую структуру. Для переменных ID1
(символьная переменная) и ID2
(числовая переменная) я использую sh, чтобы создать переменные ID1_WIND
и ID2_WIND
, которые принимают значения ID1
и ID2
соответственно, в каждом из 5-минутных интервалов в окне, начиная с -1-интервала до и + 7-интервалов после. Поскольку мой исходный фрейм данных и его подмножество, DF
, довольно длинные, я предоставляю только часть. Однако ID1
и ID2
принимают несколько значений наблюдений (поэтому я хотел бы, чтобы любая функция применялась ко всем значениям, которые принимает ID1
и ID2
)
DateTime FiveMinRet ID1 ID2 ID1_WIND ID2_WIND
106 3/1/01 08:45 5.26 0 0 0 0
107 3/1/01 08:50 4.20 0 0 0 0
108 3/1/01 08:55 5.25 0 0 0 0
109 3/1/01 09:00 -4.20 0 0 0 0
110 3/1/01 09:05 0.00 0 0 0 0
111 3/1/01 09:10 -9.46 0 0 0 0
112 3/1/01 09:15 -5.26 0 0 0 0
113 3/1/01 09:20 2.10 0 0 0 0
114 3/1/01 09:25 -2.10 0 0 0 0
115 3/1/01 09:30 -10.53 0 0 0 0
116 3/1/01 09:35 -8.43 0 0 0 0
117 3/1/01 09:40 -7.38 0 0 0 0
118 3/1/01 09:45 3.16 0 0 0 0
119 3/1/01 09:50 12.65 0 0 0 0
120 3/1/01 09:55 -7.38 0 0 CONST 4
121 3/1/01 10:00 12.64 CONST 4 CONST 4
122 3/1/01 10:05 -17.91 0 0 CONST 4
123 3/1/01 10:10 2.11 0 0 CONST 4
124 3/1/01 10:15 2.11 0 0 CONST 4
125 3/1/01 10:20 -2.11 0 0 CONST 4
126 3/1/01 10:25 -4.22 0 0 CONST 4
127 3/1/01 10:30 -8.44 0 0 CONST 4
128 3/1/01 10:35 -13.74 0 0 0 0
129 3/1/01 10:40 -4.23 0 0 0 0
130 3/1/01 10:45 5.29 0 0 0 0
131 3/1/01 10:50 2.11 0 0 0 0
Чтобы обеспечить воспроизводимый пример, что у меня есть:
structure(list(DateTime = structure(c(978511500, 978511800, 978512100,
978512400, 978512700, 978513000, 978513300, 978513600, 978513900,
978514200, 978514500, 978514800, 978515100, 978515400, 978515700,
978516000, 978516300, 978516600, 978516900, 978517200, 978517500,
978517800, 978518100, 978518400, 978518700, 978519000, 978519300,
978519600, 978519900, 978520200, 978520500, 978592500, 978592800,
978593100, 978593400, 978593700, 978594000, 978594300, 978594600,
978594900, 978595200, 978595500, 978595800, 978596100, 978596400,
978596700, 978597000, 978597300, 978597600, 978597900, 978598200,
978598500, 978598800, 978599100, 978599400, 978599700, 978600000,
978600300, 978600600, 978600900, 978601200, 978601500, 978678900,
978679200, 978679500, 978679800, 978680100, 978680400, 978680700,
978681000, 978681300, 978681600, 978681900, 978682200, 978682500,
978682800, 978683100, 978683400, 978683700, 978684000, 978684300,
978684600, 978684900, 978685200, 978685500, 978685800, 978686100,
978686400, 978686700, 978687000, 978687300, 978687600, 978687900,
978688200, 978688500, 978688800, 978689100, 978689400, 978689700,
978690000), class = c("POSIXct", "POSIXt"), tzone = "UTC"), Y = c(5.25790011587382,
4.20433052230489, 5.2529286284303, -4.20212213347933, 0, -9.46123592258874,
-5.26011268769361, 2.10437711214394, -2.10437711214394, -10.5285332951663,
-8.43081512683939, -7.38279841581008, 3.16472390425482, 12.6488896383947,
-7.37657444271511, 12.6422267154011, -17.9145419110806, 2.1092596576839,
2.10881485387136, -2.10881485387136, -4.21896430685881, -8.44327226940167,
-13.7355402228034, -4.23011850639372, 5.28736859988783, 2.11416491273708,
-6.34383611886301, 8.45755415674845, 2.11327135190509, 3.16906990451296,
2.11215546249463, -9.52935643079675, 3.17746123522894, 1.05892942334339,
-12.7145598819665, -4.24178161185194, 1.06061409655661, 26.4788588750432,
23.2435392549439, -13.7282877044335, -3.17074462308817, -2.11438842102847,
-3.17242111263182, -1.05769739376191, -5.29016570555693, -2.11685013491676,
4.23325225921839, 10.5752970938802, 6.33981424447025, -5.28289937797896,
3.17007452329925, 1.05646822770054, -14.8007215939479, -2.11617819009996,
-1.05825705162509, 8.46292232886153, 0, 1.05736188308912, 3.17141500623119,
-5.28625058564748, -3.17309220495103, 9.5162576570898, -4.19419110688588,
3.14580823650185, -7.34175920543735, -2.09863589437705, -12.6010727584481,
1.05069608712358, 8.40159679749958, 3.14877987382493, 6.29458686331255,
9.43445743232615, -6.28864919582431, 5.24081559084087, 0, -4.19243272045684,
0, -72.5975096508405, 25.3111293113938, 12.6315806269137, 1.05191185075681,
38.8431541872165, -20.9775617099864, -13.6590512436786, 1.05135888232244,
-9.46621159302649, -8.42193964928892, 2.1061499656623, 11.5759023713202,
13.6633580560681, 10.4975865193458, -5.24741576820198, 5.24741576820198,
12.5825747962782, -8.38662382734272, -19.9464661167116, -2.1019443062148,
4.20344688828934, 1.05058570249478, -2.10128178961835), ID1 = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "CONST", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "INT", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"NON", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "HOM", "0", "0", "0", "0"), ID2 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 0, 0, 0, 0)), row.names = c(NA,
-100L), class = c("tbl_df", "tbl", "data.frame"))
и ожидаемый результат:
structure(list(...1 = c(106, 107, 108, 109, 110, 111, 112, 113,
114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126,
127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 376, 377, 378,
379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391,
392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404,
405, 406, 664, 665, 666, 667, 668, 669, 670, 671, 672, 673, 674,
675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687,
688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700,
701), DateTime = structure(c(978511500, 978511800, 978512100,
978512400, 978512700, 978513000, 978513300, 978513600, 978513900,
978514200, 978514500, 978514800, 978515100, 978515400, 978515700,
978516000, 978516300, 978516600, 978516900, 978517200, 978517500,
978517800, 978518100, 978518400, 978518700, 978519000, 978519300,
978519600, 978519900, 978520200, 978520500, 978592500, 978592800,
978593100, 978593400, 978593700, 978594000, 978594300, 978594600,
978594900, 978595200, 978595500, 978595800, 978596100, 978596400,
978596700, 978597000, 978597300, 978597600, 978597900, 978598200,
978598500, 978598800, 978599100, 978599400, 978599700, 978600000,
978600300, 978600600, 978600900, 978601200, 978601500, 978678900,
978679200, 978679500, 978679800, 978680100, 978680400, 978680700,
978681000, 978681300, 978681600, 978681900, 978682200, 978682500,
978682800, 978683100, 978683400, 978683700, 978684000, 978684300,
978684600, 978684900, 978685200, 978685500, 978685800, 978686100,
978686400, 978686700, 978687000, 978687300, 978687600, 978687900,
978688200, 978688500, 978688800, 978689100, 978689400, 978689700,
978690000), class = c("POSIXct", "POSIXt"), tzone = "UTC"), Y = c(5.25790011587382,
4.20433052230489, 5.2529286284303, -4.20212213347933, 0, -9.46123592258874,
-5.26011268769361, 2.10437711214394, -2.10437711214394, -10.5285332951663,
-8.43081512683939, -7.38279841581008, 3.16472390425482, 12.6488896383947,
-7.37657444271511, 12.6422267154011, -17.9145419110806, 2.1092596576839,
2.10881485387136, -2.10881485387136, -4.21896430685881, -8.44327226940167,
-13.7355402228034, -4.23011850639372, 5.28736859988783, 2.11416491273708,
-6.34383611886301, 8.45755415674845, 2.11327135190509, 3.16906990451296,
2.11215546249463, -9.52935643079675, 3.17746123522894, 1.05892942334339,
-12.7145598819665, -4.24178161185194, 1.06061409655661, 26.4788588750432,
23.2435392549439, -13.7282877044335, -3.17074462308817, -2.11438842102847,
-3.17242111263182, -1.05769739376191, -5.29016570555693, -2.11685013491676,
4.23325225921839, 10.5752970938802, 6.33981424447025, -5.28289937797896,
3.17007452329925, 1.05646822770054, -14.8007215939479, -2.11617819009996,
-1.05825705162509, 8.46292232886153, 0, 1.05736188308912, 3.17141500623119,
-5.28625058564748, -3.17309220495103, 9.5162576570898, -4.19419110688588,
3.14580823650185, -7.34175920543735, -2.09863589437705, -12.6010727584481,
1.05069608712358, 8.40159679749958, 3.14877987382493, 6.29458686331255,
9.43445743232615, -6.28864919582431, 5.24081559084087, 0, -4.19243272045684,
0, -72.5975096508405, 25.3111293113938, 12.6315806269137, 1.05191185075681,
38.8431541872165, -20.9775617099864, -13.6590512436786, 1.05135888232244,
-9.46621159302649, -8.42193964928892, 2.1061499656623, 11.5759023713202,
13.6633580560681, 10.4975865193458, -5.24741576820198, 5.24741576820198,
12.5825747962782, -8.38662382734272, -19.9464661167116, -2.1019443062148,
4.20344688828934, 1.05058570249478, -2.10128178961835), ID1 = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "CONST", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "INT", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"NON", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "HOM", "0", "0", "0", "0"), ID2 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 0, 0, 0, 0), ID1_WIND = c("0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"CONST", "CONST", "CONST", "CONST", "CONST", "CONST", "CONST",
"CONST", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "INT",
"INT", "INT", "INT", "INT", "INT", "INT", "INT", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "NON", "NON", "NON", "NON",
"NON", "NON", "NON", "NON", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "HOM", "HOM", "HOM", "HOM", "HOM", "HOM"), ID2_WIND = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 19, 19, 19, 19, 19, 19, 19, 19, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 23, 23, 23,
23, 23, 23, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 22, 22, 22,
22, 22)), row.names = c(NA, -100L), class = c("tbl_df", "tbl",
"data.frame"))
Заранее спасибо