Hello,
The best option is package zoo, function(s) rollapply.
p <- zoo::rollapply(nordn, width = 15, FUN = max, fill = c(NA, 0, NA))
These are meant as checks, see the differences between the one-liner
above and your result.
dim(position)
length(p)
w <- which(position[, 1] == p)
position[w, ]
Hope this helps,
Rui Barradas
?s 09:59 de 13/11/20, ani jaya escreveu:> Dear r list,
>
> I try to locate any local max value and location of data that follow 7
> moving window condition, meaning that this data is largest and
> centered in 7 values to the left and 7 values to the right. I can
> solve it by using for and if function, like below:
>
> dput(nordn)
> c(`1` = 36.3167318892351, `2` = 13.6970407938013, `3` = 24.8984180253768,
> `4` = -17.6647224450612, `5` = -7.57647505851449, `6` = -25.2496137259847,
> `7` = -2.01332893598408, `8` = -7.69519958042397, `9` = -8.26353826728723,
> `10` = -0.711578256473814, `11` = -9.49356104351281, `12` =
-2.85486263863268,
> `13` = 3.34926923529002, `14` = -31.4961884481779, `15` =
-26.1167714774709,
> `16` = -42.6411928687283, `17` = -38.0270384442816, `18` =
-11.131459061204,
> `19` = -23.622369248939, `20` = -3.64517202744244, `21` = 29.9049425605881,
> `22` = 32.8660637407913, `23` = 37.5996245139886, `24` = 40.0028071315676,
> `25` = 42.1488678067843, `26` = 29.898734204143, `27` = 31.5737226769497,
> `28` = 17.1072348768252, `29` = 18.3678580553113, `30` = 2.99488592118706,
> `31` = 17.5268614959386, `32` = 4.63931611503325, `33` = 0.780891049248694,
> `34` = 1.2451803667649, `35` = -17.4138837461862, `36` = -17.1764836997295,
> `37` = -18.9385526336078, `38` = -18.8785134814426, `39` =
-18.8892579027502,
> `40` = -17.05939841442, `41` = -17.1773872762212, `42` = -13.9956678436841,
> `43` = -25.1754328292478, `44` = -14.3751251170127, `45` =
-5.72211699349243,
> `46` = -5.92331095941663, `47` = 5.70799013331376, `48` = 5.53987608486618,
> `49` = 9.93527336363547, `50` = 9.83337030810877, `51` = 9.77591432958593,
> `52` = 20.4486541160032, `53` = 20.5004918014168, `54` = 17.2771628653508,
> `55` = 14.4829910263769, `56` = 17.7218103830368, `57` = 11.5422494691249,
> `58` = 11.9634243026261, `59` = 6.45278534199771, `60` = 7.04912282719943,
> `61` = -9.36267510164784, `62` = -8.58224224917278, `63` =
-7.70790082260233,
> `64` = -23.4156448928377, `65` = -22.3528607985815, `66` =
-20.6638955663105,
> `67` = -22.8623707541409, `68` = -18.0788043293803, `69` =
-21.1362290377644,
> `70` = -19.6291679260569, `71` = -18.4864526754101, `72` =
-16.8268389833748,
> `73` = -1.86517468137026e-13, `74` = -1.86517468137026e-13, `75` >
-1.86517468137026e-13,
> `76` = -1.86517468137026e-13, `77` = -1.86517468137026e-13, `78` >
-1.86517468137026e-13,
> `79` = -1.86517468137026e-13, `80` = -1.86517468137026e-13, `81` >
-1.86517468137026e-13,
> `82` = -1.86517468137026e-13, `83` = -1.86517468137026e-13, `84` >
-1.86517468137026e-13,
> `85` = -1.86517468137026e-13, `86` = -1.86517468137026e-13, `87` >
-1.86517468137026e-13,
> `88` = 119.769379215677, `89` = 121.143950270482, `90` = -3.3158245341025,
> `91` = -2.27565861892447, `92` = -4.73724890799339, `93` =
-1.99736436960567,
> `94` = -3.75331767972433, `95` = -5.95256351982827, `96` =
-6.15867775456779,
> `97` = -10.6570276388287, `98` = -11.5475819732368, `99` =
-12.8244899190047,
> `100` = -7.67798939908287, `101` = -9.82140578284223, `102` =
-9.59888007628085,
> `103` = -12.7414099822392, `104` = -22.3056908542011, `105` =
-12.0279597037476,
> `106` = -31.499265969641, `107` = -1.86517468137026e-13, `108` >
-1.86517468137026e-13,
> `109` = -1.86517468137026e-13, `110` = -1.86517468137026e-13,
> `111` = -1.86517468137026e-13, `112` = -1.86517468137026e-13,
> `113` = -1.86517468137026e-13, `114` = -1.86517468137026e-13,
> `115` = -1.86517468137026e-13, `116` = -1.86517468137026e-13,
> `117` = 32.862745948818, `118` = -1.86517468137026e-13)
>
>
> position<-matrix(NA,118,2)
> for (i in 7:(length(nordn)-7)){
> if (nordn[i]>nordn[i+1]&&
> nordn[i]>nordn[i+2]&&
> nordn[i]>nordn[i+3]&&
> nordn[i]>nordn[i+4]&&
> nordn[i]>nordn[i+5]&&
> nordn[i]>nordn[i+6]&&
> nordn[i]>nordn[i+7]&&
> nordn[i]>nordn[i-7]&&
> nordn[i]>nordn[i-6]&&
> nordn[i]>nordn[i-5]&&
> nordn[i]>nordn[i-4]&&
> nordn[i]>nordn[i-3]&&
> nordn[i]>nordn[i-2]&&
> nordn[i]>nordn[i-1]){
> position[i,1]<-nordn[i]
> position[i,2]<-i
> }
> }
>
> Is there any straight way or lead or function should I check? And can
> I just get the value and the position without storing NA value? Thank
> you.
>
> Ani
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>