在 R 的函数中使用 RLE

Using RLE in a function in R

我有以下数据:

 dat2<-structure(list(year = c(1979L, 1979L, 1979L, 1979L, 1979L, 
 1979L,1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 
 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 
 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 
 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 
 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 
 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 
 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 
 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 
 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 
 1979L, 1979L, 1979L, 1979L, 1979L), mon = c(5L, 5L, 5L, 5L, 5L, 
 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 
 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
 7L, 7L, 7L, 7L, 7L, 7L, 7L), day = c(1L, 2L, 3L, 4L, 5L, 6L, 
 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 
 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 
 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 
 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 
 26L, 27L, 28L, 29L, 30L, 31L), phase = c(2L, 3L, 3L, 3L, 3L, 
 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 
 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 
 8L, 8L, 8L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
 3L, 3L, 4L, 4L, 4L, 4L, 5L), Rainfall = c(0, 0.25, 0, 0, 0, 0, 
 0, 0, 0, 0, 0, 19.2, 11.125, 1.95, 0.125, 23.2, 35.575, 37.4, 
 6.425, 10.275, 3.05, 50.075, 23.05, 2, 1.4, 3.325, 5.8, 13.375, 
 27.725, 14.3, 20.9, 5.075, 11.5, 0.825, 0.9, 0.95, 1, 0.075, 
 0.025, 1.15, 0.325, 0, 0, 0.325, 1.925, 2.15, 6.55, 3.15, 2.15, 
 1.725, 0.575, 4.875, 3, 3.6, 3.95, 14.35, 7.625, 9.2, 9.275, 
 18.375, 6.525, 0.36, 0.1, 75.04, 38.56, 1.18, 1.16, 4.12, 5.7, 
 5, 0, 1.36, 0, 5.18, 0.64, 2.68, 0.36, 0.3, 0, 3.56, 9.62, 0.52, 
 1.26, 17.04, 16.3, 2.84, 10.2, 52.98, 51.76, 15.06, 19.62, 19.46
 )), row.names = c(NA, 92L), class = "data.frame")

此数据集中有四列(年、月、日、相位和降雨量)。

我想数一数:

(1) “降雨量”至少连续 3 天低于 5 mm/day

(2) 阶段是“阶段 1”

我不确定如何为此正确应用 RLE 函数。 到目前为止,我有以下脚本但不包含第二个条件:

dat2<-dat[,c("phase","Rainfall")]
countruns = function(x){
RLE = rle(x$Rainfall<5)   
sum(RLE$lengths==1)
}

sum() 应该给出满足上述 2 个条件的总计数。

我将不胜感激。

林兹

这是否达到了你想要的效果?

library(tidyverse)

dat_out <- dat2 %>%
  mutate(phase1_T = phase == 1,
         rainfall_T = Rainfall < 5 & lag(Rainfall, n = 1, default = 5) < 5 & lag(Rainfall, n = 2, default = 5) < 5,
         both_conditions = phase1_T & rainfall_T)

sum(dat_out$both_conditions)

如果您需要将其放入评论中要求的函数中,您可以执行以下操作:

library(tidyverse)

contruns <- function(.df){
  dat_out <- .df %>%
    mutate(phase1_T = phase == 1,
           rainfall_T = Rainfall < 5 & lag(Rainfall, n = 1, default = 5) < 5 & lag(Rainfall, n = 2, default = 5) < 5,
           both_conditions = phase1_T & rainfall_T)
  
  sum(dat_out$both_conditions)
}

contruns(dat2)

首先子集为 phase == 1 然后 Rainfall 至少连续 3 天低于 5 mm/day 的方法怎么样。

# Subset to phase == 1
dat_p1 <- subset(dat2, phase == 1)

# Find cases where Rainfall is below 5 mm/day for at least 3 consecutive days
rainfall_rle_df <- data.frame(unclass(rle(dat_p1$Rainfall < 5)))
nrow(subset(rainfall_rle_df, lengths >= 3 & values == TRUE))

#----
[1] 1