R,选择降雨事件并根据时间序列数据计算降雨事件总数

时间:2018-07-16 23:04:40

标签: r

这就是我要使代码起作用的地方:

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

-我的攻击计划是创建一列,其中将包含事件的唯一“标志”。事件标志或ID可以是事件的开始时间戳,也可以是最后一个标识符(1,1,1,1,2,2,2,2,2)的n + 1等。唯一的标志部分,因为我需要R在“沉淀”列中“向前看”,以查看将来是否会在6个小时内下雨。然后,如果这样做,则应创建一个标志。

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

示例输出

事件ID沉淀(输入)事件状态事件停止时间(小时)

1 0.07 10/6/2017 17:00 10/6/2017 22:00 6:00

2 0.01 10/7/2017 15:00 10/7/2017 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") 

原始数据

DateTime 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

10/7/2017 0:00 0

10/7/2017 1:00 0

10/7/2017 2:00 0

10/7/2017 3:00 0

10/7/2017 4:00 0

10/7/2017 5:00 0

10/7/2017 6:00 0

10/7/2017 7:00 0

10/7/2017 8:00 0

10/7/2017 9:00 0

10/7/2017 10:00 0

10/7/2017 11:00 0

10/7/2017 12:00 0

10/7/2017 13:00 0

10/7/2017 14:00 0

10/7/2017 15:00 0.01

1 个答案:

答案 0 :(得分:0)

如果有人仍在寻找解决此问题的方法,这是我的“整洁”方法。我将数据保存在名为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

相关问题