首页 > 解决方案 > R,选择降雨事件并根据时间序列数据计算降雨事件总数

问题描述

这是我试图使代码执行的操作:

- 识别数据集中独特的降雨“事件”。我想从事件之间的 6 个干燥小时的事件间期开始。

- 我的攻击计划是创建一个包含事件的唯一“标志”的列。事件标志或 ID 可能是事件的开始时间戳,或者只是最后一个标识符 (1,1,1,1,2,2,2,2) 等的 n+1。我很难得到这个独特的标志部分,因为我需要 R 在 precip 列中“向前看”,以查看未来 6 小时内是否下雨。如果是这样,它应该创建一个标志。

-最后,我想得到一个输出(类似于数据透视表),它总结了每个独特事件的总降水量(以英寸为单位),还给了我开始和停止时间,以及事件的总持续时间。

示例输出

事件 ID Precip (in) 事件开始 事件停止时间(小时)

1 0.07 2017 年 10 月 6 日 17:00 2017 年 10 月 6 日 22:00 6:00

2 0.01 2017 年 10 月 7 日 15:00 2017 年 10 月 7 日 15:00 1:00

3 0.15 10/10/2017 11:00 10/10/2017 13:00 3:00

CODE
library(zoo) # to get rollsum fxn

DF1 <- read.csv("U:/R_files/EOF_Rainfall_Stats_2017- 
18/Precip_DF1_Oct17toMay18.csv")

DF1$event <- NA

DF1$event[DF1$Precip_in > 0] = "1"
DF1$event[DF1$Precip_in == 0] = "0"
str(DF1)
DF1$event <- as.numeric(DF1$event)
str(DF1)


DF1$rollsum6 <- round(rollsum(DF1$event, k=6, fill=NA, align="right"),5)


DF1$eventID <- NA
DF1$eventID <- ifelse(DF1$rollsum6 >= 2 & DF1$event == 1, "flag", "NA") 

原始数据

日期时间 Precip_in

2017 年 10 月 6 日 13:00 0

2017 年 10 月 6 日 14:00 0

2017 年 10 月 6 日 15:00 0

2017 年 10 月 6 日 16:00 0

2017 年 10 月 6 日 17:00 0.04

2017 年 10 月 6 日 18:00 0

2017 年 10 月 6 日 19:00 0

2017 年 10 月 6 日 20:00 0

2017 年 10 月 6 日 21:00 0.01

2017 年 10 月 6 日 22:00 0.02

2017 年 10 月 6 日 23:00 0

2017 年 10 月 7 日 0:00 0

2017 年 10 月 7 日 1:00 0

2017 年 10 月 7 日 2:00 0

2017 年 10 月 7 日 3:00 0

2017 年 10 月 7 日 4:00 0

2017 年 10 月 7 日 5:00 0

2017 年 10 月 7 日 6:00 0

2017 年 10 月 7 日 7:00 0

2017 年 10 月 7 日 8:00 0

2017 年 10 月 7 日 9:00 0

2017 年 10 月 7 日 10:00 0

2017 年 10 月 7 日 11:00 0

2017 年 10 月 7 日 12:00 0

2017 年 10 月 7 日 13:00 0

2017 年 10 月 7 日 14:00 0

2017 年 10 月 7 日 15:00 0.01

标签: r

解决方案


If someone is still looking for a way to solve this question, here is my 'tidy' approach on it. I saved the data in a variable called data.

library(dplyr)

# Set data column as POSIXct, important for calculating duration afterwards
data <- data %>% mutate(DateTime = as.POSIXct(DateTime, format = '%m/%d/%Y %H:%M'))

flags <- data %>% 
  # Set a rain flag if there is rain registered on the gauge
  mutate(rainflag = ifelse(Precip_in > 0, 1, 0)) %>% 
  # Create a column that contains the number of consecutive times there was rain or not.
  # Use `rle`` which indicates how many times consecutive values happen, and `rep`` to repeat it for each row.
  mutate(rainlength = rep(rle(rainflag)$lengths, rle(rainflag)$lengths)) %>% 
  # Set a flag for an event happening, when there is rain there is a rain event, 
  # when it is 0 but not for six consecutive times, it is still a rain event
  mutate(
    eventflag = ifelse(
      rainflag == 1, 
      1, 
      ifelse(
        rainflag == 0 & rainlength < 6, 
        1, 
        0
      )
    )
  ) %>% 
  # Correct for the case when the dataset starts with no rain for less than six consecutive times
  # If within the first six rows there is no rain registered, then the event flag should change to 0
  mutate(eventflag = ifelse(row_number() < 6 & rainflag == 0, 0, eventflag)) %>% 
  # Add an id to each event (rain or not), to group by on the pivot table
  mutate(eventid = rep(seq(1,length(rle(eventflag)$lengths)), rle(eventflag)$lengths))

rain_pivot <- flags %>% 
  # Select only the rain events
  filter(eventflag == 1) %>% 
  # Group by id
  group_by(eventid) %>% 
  summarize(
    precipitation = sum(Precip_in),
    eventStart = first(DateTime),
    eventEnd = last(DateTime)
  ) %>% 
  # Compute time difference as duration of event, add 1 hour, knowing that the timestamp is the time when the rain record ends
  mutate(time = as.numeric(difftime(eventEnd,eventStart, units = 'h')) + 1)

rain_pivot
#> # A tibble: 2 x 5
#>   eventid precipitation eventStart          eventEnd             time
#>     <int>         <dbl> <dttm>              <dttm>              <dbl>
#> 1       2          0.07 2017-10-06 17:00:00 2017-10-06 22:00:00     6
#> 2       4          0.01 2017-10-07 15:00:00 2017-10-07 15:00:00     1


推荐阅读