Если вы всегда можете рассчитывать на интервал в один месяц, давайте временно отбросим информацию о времени:
temps <- Nino3.4_1974_2000$Nino3.4_degree_1974_2000_plain
Итак, поскольку каждая температура в этом векторе всегда разделена одним месяцем, нам просто нужно искать прогоны, где temps[i]>=0.5
, и прогон должен быть не менее 5 длин.
Если мы сделаем следующее:
ofinterest <- temps >= 0.5
у нас будет вектор ofinterest
со значениями TRUE FALSE FALSE TRUE TRUE ....
и т. Д., Где он TRUE
, когда temps[i]
был> = 0,5, а FALSE
в противном случае.
Чтобы перефразировать вашу проблему, нам просто нужно найти вхождения не менее пяти TRUE
подряд .
Для этого мы можем использовать функцию rle
. ?rle
дает:
> ?rle
Description
Compute the lengths and values of runs of equal values in a vector
- or the reverse operation.
Value:
‘rle()’ returns an object of class ‘"rle"’ which is a list with
components:
lengths: an integer vector containing the length of each run.
values: a vector of the same length as ‘lengths’ with the
corresponding values.
Таким образом, мы используем rle
, который подсчитывает все серии подряд TRUE
подряд и последовательно FALSE
подряд, и ищет по крайней мере 5 TRUE
подряд.
Я просто сделаю некоторые данные для демонстрации:
# for you, temps <- Nino3.4_1974_2000$Nino3.4_degree_1974_2000_plain
temps <- runif(1000)
# make a vector that is TRUE when temperature is >= 0.5 and FALSE otherwise
ofinterest <- temps >= 0.5
# count up the runs of TRUEs and FALSEs using rle:
runs <- rle(ofinterest)
# we need to find points where runs$lengths >= 5 (ie more than 5 in a row),
# AND runs$values is TRUE (so more than 5 'TRUE's in a row).
streakIs <- which(runs$lengths>=5 & runs$values)
# these are all the el_nino occurences.
# We need to convert `streakIs` into indices into our original `temps` vector.
# To do this we add up all the `runs$lengths` up to `streakIs[i]` and that gives
# the index into `temps`.
# that is:
# startMonths <- c()
# for ( n in streakIs ) {
# startMonths <- c(startMonths, sum(runs$lengths[1:(n-1)]) + 1
# }
#
# However, since this is R we can vectorise with sapply:
startMonths <- sapply(streakIs, function(n) sum(runs$lengths[1:(n-1)])+1)
Теперь, если вы сделаете Nino3.4_1974_2000$Month_common[startMonths]
, вы получите все месяцы, в которые начинался Эль-Ниньо.
Это сводится к нескольким строкам:
runs <- rle(Nino3.4_1974_2000$Nino3.4_degree_1974_2000_plain>=0.5)
streakIs <- which(runs$lengths>=5 & runs$values)
startMonths <- sapply(streakIs, function(n) sum(runs$lengths[1:(n-1)])+1)
Nino3.4_1974_2000$Month_common[startMonths]