下面是我试图让代码实现的功能:
-identify数据集中唯一的降雨“事件”。我想从两个事件之间6个小时的事件间隔开始。
-My的攻击计划是创建一个列,该列将包含事件的唯一“标志”。事件标志或ID可以是事件的开始时间戳,或者仅仅是最后一个标识符(1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1)的n+1。我在获取这个唯一的标志部分时遇到了麻烦,因为我需要R在precip列中“向前看”,看看未来6小时内是否会下雨。如果是这样,它应该创建一个标志。
输出示例
Event ID Precip (in) Event STart事件停止时间(小时)
1 0.07 10-6 17:00 10-6 22:00 6:00
2 0.01 2017 10-7 15:00 10-7 15:00 1:00
3 0.15 10-10 11:00 10-10 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
10/6/2017 21:00 0.01
10/6/2017 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
发布于 2019-11-25 19:52:20
如果有人还在寻找解决这个问题的方法,这里是我的“整洁”方法。我将数据保存在一个名为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
https://stackoverflow.com/questions/51371155
复制相似问题