【问题标题】:R, select rainfall events and calculate rainfall event total from time-series dataR,选择降雨事件并根据时间序列数据计算降雨事件总数
【发布时间】:2018-07-16 23:04:40
【问题描述】:

这是我试图让代码做的事情:

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

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

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

示例输出

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

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") 

原始数据

日期时间 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

10/7/2017 7:00 0

2017 年 10 月 7 日 8:00 0

2017 年 10 月 7 日 9:00 0

10/7/2017 10:00 0

2017 年 10 月 7 日 11:00 0

2017 年 10 月 7 日 12:00 0

2017 年 10 月 7 日 13:00 0

10/7/2017 14:00 0

2017 年 10 月 7 日 15:00 0.01

【问题讨论】:

  • 请根据示例数据修改您的帖子以包含预期输出。还可以使用编辑工具正确排版您的代码。
  • 到目前为止你有没有尝试过?如果我们从现有的代码库开始,纠正问题而不是从头开始创建,它会有所帮助。

标签: r


【解决方案1】:

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

【讨论】:

  • 非常感谢,这正是我想要的。我会稍微修改你的代码,因为例如我只想考虑降水量超过 0.3 毫米的降雨事件。我认为我们可以把它变成一个函数。将在接下来的几天发布我的更新。
猜你喜欢
  • 2021-09-15
  • 1970-01-01
  • 1970-01-01
  • 2017-02-24
  • 2018-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-07
相关资源
最近更新 更多