Ben Ben - 1 month ago 6
R Question

Find 3 consecutive highest values in circular series in R

i have data in the following form (2 examples):

p1 <- structure(c(1.38172177074188, 1.18601365390563, 1.25131938561825,
1.07175353794277, 0.887770295772917, 0.806599968169486, 0.843543355495394,
0.889051695167723, 0.764131945540256, 0.699309441111923, 0.945165791967098,
1.31310409471336), .Dim = 12L)

p2 <- structure(c(1.24801075135611, 1.06280347993594, 1.21410288703334,
1.36797720634294, 1.07291218307332, 0.936924063490867, 0.819723966406961,
0.854960740335283, 0.718565087630857, 0.649827141012991, 0.785853771875901,
1.04368795443605), .Dim = 12L)


These are standardized monthly means of hydrological time series; so-called Pardé regimes that give some indication about annual seasonality. To do further analysis, i need to derive the 3 highest and lowest months from these Pardé series. Because seasonality can be bimodal, i need to identify the 3 highest/lowest consecutive data points (which are most often not the three absolute highest/lowest data points, see examples) to derive the timing of the most wet and dry periods. Up to now i failed because of the circular character of the time series, which poses a special challenge.
Any suggestions?

Answer

You could use filter. It sums consecutive values and can deal with circular time series.

f1 <- stats::filter(p1, c(1, 1, 1), circular = TRUE, sides = 1)
#Time Series:
#  Start = 1 
#End = 12 
#Frequency = 1 
#[1] 3.639992 3.880840 3.819055 3.509087 3.210843 2.766124 2.537914 2.539195 2.496727 2.352493 2.408607 2.957579
((which.max(f1) - (3:1)) %% 12) + 1
#[1] 12  1  2