Расширение наблюдений переменной до windows - PullRequest
1 голос
/ 10 июля 2020

Подмножество моего исходного фрейма данных, 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"))

Заранее спасибо

Ответы [ 2 ]

2 голосов
/ 10 июля 2020

Вы можете собрать желаемые индексы и значения для ненулевого элемента в столбцах DF1 и DF2 и применить их к пустым столбцам:

DF$ID1_WIND <- rep("0", nrow(DF))
DF$ID2_WIND <- numeric(nrow(DF))

rep_list <- lapply(which(DF$ID1 != "0"), function(x){
  list(ind = (x - 1):(x + 7), val1 = DF$ID1[x], val2 = DF$ID2[x])
  })

for(i in seq_along(rep_list))
{
  ss <- rep_list[[i]]$ind[rep_list[[i]]$ind <= nrow(DF)]
  DF$ID1_WIND[ss] <- rep_list[[i]]$val1
  DF$ID2_WIND[ss] <- rep_list[[i]]$val2
}

Что, я думаю, дает желаемый результат :

as.data.frame(DF)
#>                DateTime          Y   ID1 ID2 ID1_WIND ID2_WIND
#> 1   2001-01-03 08:45:00   5.257900     0   0        0        0
#> 2   2001-01-03 08:50:00   4.204331     0   0        0        0
#> 3   2001-01-03 08:55:00   5.252929     0   0        0        0
#> 4   2001-01-03 09:00:00  -4.202122     0   0        0        0
#> 5   2001-01-03 09:05:00   0.000000     0   0        0        0
#> 6   2001-01-03 09:10:00  -9.461236     0   0        0        0
#> 7   2001-01-03 09:15:00  -5.260113     0   0        0        0
#> 8   2001-01-03 09:20:00   2.104377     0   0        0        0
#> 9   2001-01-03 09:25:00  -2.104377     0   0        0        0
#> 10  2001-01-03 09:30:00 -10.528533     0   0        0        0
#> 11  2001-01-03 09:35:00  -8.430815     0   0        0        0
#> 12  2001-01-03 09:40:00  -7.382798     0   0        0        0
#> 13  2001-01-03 09:45:00   3.164724     0   0        0        0
#> 14  2001-01-03 09:50:00  12.648890     0   0        0        0
#> 15  2001-01-03 09:55:00  -7.376574     0   0    CONST        4
#> 16  2001-01-03 10:00:00  12.642227 CONST   4    CONST        4
#> 17  2001-01-03 10:05:00 -17.914542     0   0    CONST        4
#> 18  2001-01-03 10:10:00   2.109260     0   0    CONST        4
#> 19  2001-01-03 10:15:00   2.108815     0   0    CONST        4
#> 20  2001-01-03 10:20:00  -2.108815     0   0    CONST        4
#> 21  2001-01-03 10:25:00  -4.218964     0   0    CONST        4
#> 22  2001-01-03 10:30:00  -8.443272     0   0    CONST        4
#> 23  2001-01-03 10:35:00 -13.735540     0   0    CONST        4
#> 24  2001-01-03 10:40:00  -4.230119     0   0        0        0
#> 25  2001-01-03 10:45:00   5.287369     0   0        0        0
#> 26  2001-01-03 10:50:00   2.114165     0   0        0        0
#> 27  2001-01-03 10:55:00  -6.343836     0   0        0        0
#> 28  2001-01-03 11:00:00   8.457554     0   0        0        0
#> 29  2001-01-03 11:05:00   2.113271     0   0        0        0
#> 30  2001-01-03 11:10:00   3.169070     0   0        0        0
#> 31  2001-01-03 11:15:00   2.112155     0   0        0        0
#> 32  2001-01-04 07:15:00  -9.529356     0   0        0        0
#> 33  2001-01-04 07:20:00   3.177461     0   0        0        0
#> 34  2001-01-04 07:25:00   1.058929     0   0        0        0
#> 35  2001-01-04 07:30:00 -12.714560     0   0        0        0
#> 36  2001-01-04 07:35:00  -4.241782     0   0        0        0
#> 37  2001-01-04 07:40:00   1.060614     0   0        0        0
#> 38  2001-01-04 07:45:00  26.478859     0   0        0        0
#> 39  2001-01-04 07:50:00  23.243539     0   0        0        0
#> 40  2001-01-04 07:55:00 -13.728288     0   0        0        0
#> 41  2001-01-04 08:00:00  -3.170745     0   0        0        0
#> 42  2001-01-04 08:05:00  -2.114388     0   0        0        0
#> 43  2001-01-04 08:10:00  -3.172421     0   0        0        0
#> 44  2001-01-04 08:15:00  -1.057697     0   0        0        0
#> 45  2001-01-04 08:20:00  -5.290166     0   0        0        0
#> 46  2001-01-04 08:25:00  -2.116850     0   0      INT       19
#> 47  2001-01-04 08:30:00   4.233252   INT  19      INT       19
#> 48  2001-01-04 08:35:00  10.575297     0   0      INT       19
#> 49  2001-01-04 08:40:00   6.339814     0   0      INT       19
#> 50  2001-01-04 08:45:00  -5.282899     0   0      INT       19
#> 51  2001-01-04 08:50:00   3.170075     0   0      INT       19
#> 52  2001-01-04 08:55:00   1.056468     0   0      INT       19
#> 53  2001-01-04 09:00:00 -14.800722     0   0      INT       19
#> 54  2001-01-04 09:05:00  -2.116178     0   0      INT       19
#> 55  2001-01-04 09:10:00  -1.058257     0   0        0        0
#> 56  2001-01-04 09:15:00   8.462922     0   0        0        0
#> 57  2001-01-04 09:20:00   0.000000     0   0        0        0
#> 58  2001-01-04 09:25:00   1.057362     0   0        0        0
#> 59  2001-01-04 09:30:00   3.171415     0   0        0        0
#> 60  2001-01-04 09:35:00  -5.286251     0   0        0        0
#> 61  2001-01-04 09:40:00  -3.173092     0   0        0        0
#> 62  2001-01-04 09:45:00   9.516258     0   0        0        0
#> 63  2001-01-05 07:15:00  -4.194191     0   0        0        0
#> 64  2001-01-05 07:20:00   3.145808     0   0        0        0
#> 65  2001-01-05 07:25:00  -7.341759     0   0        0        0
#> 66  2001-01-05 07:30:00  -2.098636     0   0        0        0
#> 67  2001-01-05 07:35:00 -12.601073     0   0        0        0
#> 68  2001-01-05 07:40:00   1.050696     0   0        0        0
#> 69  2001-01-05 07:45:00   8.401597     0   0        0        0
#> 70  2001-01-05 07:50:00   3.148780     0   0        0        0
#> 71  2001-01-05 07:55:00   6.294587     0   0        0        0
#> 72  2001-01-05 08:00:00   9.434457     0   0        0        0
#> 73  2001-01-05 08:05:00  -6.288649     0   0        0        0
#> 74  2001-01-05 08:10:00   5.240816     0   0        0        0
#> 75  2001-01-05 08:15:00   0.000000     0   0        0        0
#> 76  2001-01-05 08:20:00  -4.192433     0   0        0        0
#> 77  2001-01-05 08:25:00   0.000000     0   0      NON       23
#> 78  2001-01-05 08:30:00 -72.597510   NON  23      NON       23
#> 79  2001-01-05 08:35:00  25.311129     0   0      NON       23
#> 80  2001-01-05 08:40:00  12.631581     0   0      NON       23
#> 81  2001-01-05 08:45:00   1.051912     0   0      NON       23
#> 82  2001-01-05 08:50:00  38.843154     0   0      NON       23
#> 83  2001-01-05 08:55:00 -20.977562     0   0      NON       23
#> 84  2001-01-05 09:00:00 -13.659051     0   0      NON       23
#> 85  2001-01-05 09:05:00   1.051359     0   0      NON       23
#> 86  2001-01-05 09:10:00  -9.466212     0   0        0        0
#> 87  2001-01-05 09:15:00  -8.421940     0   0        0        0
#> 88  2001-01-05 09:20:00   2.106150     0   0        0        0
#> 89  2001-01-05 09:25:00  11.575902     0   0        0        0
#> 90  2001-01-05 09:30:00  13.663358     0   0        0        0
#> 91  2001-01-05 09:35:00  10.497587     0   0        0        0
#> 92  2001-01-05 09:40:00  -5.247416     0   0        0        0
#> 93  2001-01-05 09:45:00   5.247416     0   0        0        0
#> 94  2001-01-05 09:50:00  12.582575     0   0        0        0
#> 95  2001-01-05 09:55:00  -8.386624     0   0      HOM       22
#> 96  2001-01-05 10:00:00 -19.946466   HOM  22      HOM       22
#> 97  2001-01-05 10:05:00  -2.101944     0   0      HOM       22
#> 98  2001-01-05 10:10:00   4.203447     0   0      HOM       22
#> 99  2001-01-05 10:15:00   1.050586     0   0      HOM       22
#> 100 2001-01-05 10:20:00  -2.101282     0   0      HOM       22

Создано 2020-07-10 пакетом REPEX (v0.3.0)

1 голос
/ 10 июля 2020

A dplyr способ, который, я уверен, можно упростить:


library(dplyr)

df1 <- 
  df %>% 
  mutate(ID1_WIND = case_when(lead(ID1) != "0" ~ lead(ID1),
                              lag(ID1) != "0" ~ lag(ID1),
                              lag(ID1, 2) != "0" ~ lag(ID1, 2),
                              lag(ID1, 3) != "0" ~ lag(ID1, 3),
                              lag(ID1, 4) != "0" ~ lag(ID1, 4),
                              lag(ID1, 5) != "0" ~ lag(ID1, 5),
                              lag(ID1, 6) != "0" ~ lag(ID1, 6),
                              TRUE ~ ID1),
         ID2_WIND = case_when(lead(ID2) != "0" ~ lead(ID2),
                              lag(ID2) != "0" ~ lag(ID2),
                              lag(ID2, 2) != "0" ~ lag(ID2, 2),
                              lag(ID2, 3) != "0" ~ lag(ID2, 3),
                              lag(ID2, 4) != "0" ~ lag(ID2, 4),
                              lag(ID2, 5) != "0" ~ lag(ID2, 5),
                              lag(ID2, 6) != "0" ~ lag(ID2, 6),
                              TRUE ~ ID2))


head(df1, 20)
#> # A tibble: 20 x 6
#>    DateTime                 Y ID1     ID2 ID1_WIND ID2_WIND
#>    <dttm>               <dbl> <chr> <dbl> <chr>       <dbl>
#>  1 2001-01-03 08:45:00   5.26 0         0 0               0
#>  2 2001-01-03 08:50:00   4.20 0         0 0               0
#>  3 2001-01-03 08:55:00   5.25 0         0 0               0
#>  4 2001-01-03 09:00:00  -4.20 0         0 0               0
#>  5 2001-01-03 09:05:00   0    0         0 0               0
#>  6 2001-01-03 09:10:00  -9.46 0         0 0               0
#>  7 2001-01-03 09:15:00  -5.26 0         0 0               0
#>  8 2001-01-03 09:20:00   2.10 0         0 0               0
#>  9 2001-01-03 09:25:00  -2.10 0         0 0               0
#> 10 2001-01-03 09:30:00 -10.5  0         0 0               0
#> 11 2001-01-03 09:35:00  -8.43 0         0 0               0
#> 12 2001-01-03 09:40:00  -7.38 0         0 0               0
#> 13 2001-01-03 09:45:00   3.16 0         0 0               0
#> 14 2001-01-03 09:50:00  12.6  0         0 0               0
#> 15 2001-01-03 09:55:00  -7.38 0         0 CONST           4
#> 16 2001-01-03 10:00:00  12.6  CONST     4 CONST           4
#> 17 2001-01-03 10:05:00 -17.9  0         0 CONST           4
#> 18 2001-01-03 10:10:00   2.11 0         0 CONST           4
#> 19 2001-01-03 10:15:00   2.11 0         0 CONST           4
#> 20 2001-01-03 10:20:00  -2.11 0         0 CONST           4

Создано 10.07.2020 с помощью пакета (v0.3.0 )

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...