0

我有一个大的时间序列(以数据框形式)(n => 6000),如下所示:

              time, precip

1   2005-09-30 11:45:00,   0.08
2   2005-09-30 23:45:00,   0.72
3   2005-10-01 11:45:00,   0.01
4   2005-10-01 23:45:00,   0.08
5   2005-10-02 11:45:00,   0.10
6   2005-10-02 23:45:00,   0.33
7   2005-10-03 11:45:00,   0.15
8   2005-10-03 23:45:00,   0.30
9   2005-10-04 11:45:00,   0.00
10  2005-10-04 23:45:00,   0.00
11  2005-10-05 11:45:00,   0.02
12  2005-10-05 23:45:00,   0.00
13  2005-10-06 11:45:00,   0.00
14  2005-10-06 23:45:00,   0.01
15  2005-10-07 11:45:00,   0.00
16  2005-10-07 23:45:00,   0.00
17  2005-10-08 11:45:00,   0.00
18  2005-10-08 23:45:00,   0.16
19  2005-10-09 11:45:00,   0.03
20  2005-10-09 23:45:00,   0.00

每行都有一个时间(YYYY-MM-DD HH:MM:SS,12 小时时间序列)和降水量。我想按风暴事件分离数据。

我想做的是:1)添加一个名为“风暴”的新列2)为每组以0分隔的金额值,称之为一个风暴。

例如...

             Time,        Precip,  Storm

1   2005-09-30 11:45:00,   0.08,  1
2   2005-09-30 23:45:00,   0.72,  1
3   2005-10-01 11:45:00,   0.01,  1
4   2005-10-01 23:45:00,  0.08,  1
5   2005-10-02 11:45:00,   0.10,  1
6   2005-10-02 23:45:00,   0.33,  1
7   2005-10-03 11:45:00,   0.15, 1
8   2005-10-03 23:45:00,   0.30, 1
9   2005-10-04 11:45:00,   0.00
10  2005-10-04 23:45:00,   0.00
11  2005-10-05 11:45:00,   0.02, 2
12  2005-10-05 23:45:00,   0.00
13  2005-10-06 11:45:00,   0.00
14  2005-10-06 23:45:00,   0.01, 3
15  2005-10-07 11:45:00,   0.00
16  2005-10-07 23:45:00,   0.00
17  2005-10-08 11:45:00,   0.00
18  2005-10-08 23:45:00,   0.16, 4
19  2005-10-09 11:45:00,   0.03, 4
20  2005-10-09 23:45:00,   0.00

4)在那之后,我的计划是通过风暴事件对数据进行子集化。

我对 R 很陌生,所以不要害怕指出显而易见的事情。您的帮助将不胜感激!

4

2 回答 2

4

您可以找到风暴中的事件,然后使用rle和修改结果

# assuming your data is called rainfall
# identify whether a  precipitation has been recorded at each timepoint
rainfall$storm <- rainfall$precip > 0
# do run length encoding on this storm indicator
storms < rle(rainfall$storms)
# set the FALSE values to NA
is.na(storms$values) <- !storms$values
# replace the TRUE values with a number in seqence
storms$values[which(storms$values)] <- seq_len(sum(storms$values, na.rm = TRUE))
# use inverse.rle to revert to the full length column
rainfall$stormNumber <- inverse.rle(storms)
于 2013-04-30T23:06:14.657 回答
2

假设这个输入:

Lines <- "time, precip
1   2005-09-30 11:45:00,   0.08
2   2005-09-30 23:45:00,   0.72
3   2005-10-01 11:45:00,   0.01
4   2005-10-01 23:45:00,  0.08
5   2005-10-02 11:45:00,   0.10
6   2005-10-02 23:45:00,   0.33
7   2005-10-03 11:45:00,   0.15
8   2005-10-03 23:45:00,   0.30
9   2005-10-04 11:45:00,   0.00
10  2005-10-04 23:45:00,   0.00
11  2005-10-05 11:45:00,   0.02
12  2005-10-05 23:45:00,   0.00
13  2005-10-06 11:45:00,   0.00
14  2005-10-06 23:45:00,   0.01
15  2005-10-07 11:45:00,   0.00
16  2005-10-07 23:45:00,   0.00
17  2005-10-08 11:45:00,   0.00
18  2005-10-08 23:45:00,   0.16
19  2005-10-09 11:45:00,   0.03
20  2005-10-09 23:45:00,   0.00
"

我们读入数据,然后为先验值为零的每个非零沉淀创建一个逻辑向量,该向量为 TRUE。我们添加第一个值,如果非零,则为 TRUE,如果z[1]为零,则为 FALSE。应用于cumsum该向量会在对应于非零值的位置给出正确的precip值。为了处理位置对应于零precip值的值,我们replace用来存储empty它们:

# read in data
library(zoo)
z <- read.zoo(text = Lines, skip = 1, tz = "", index = 2:3)[, 2]

# calculate
e <- NA # empty
cbind(precip = z, storm = replace(cumsum(c(z[1]!=0, z!=0 & lag(z,-1)==0)), z==0, e))

最后一行给出了这个:

                  precip storm
2005-09-30 11:45:00 0.08     1
2005-09-30 23:45:00 0.72     1
2005-10-01 11:45:00 0.01     1
2005-10-01 23:45:00 0.08     1
2005-10-02 11:45:00 0.10     1
2005-10-02 23:45:00 0.33     1
2005-10-03 11:45:00 0.15     1
2005-10-03 23:45:00 0.30     1
2005-10-04 11:45:00 0.00    NA
2005-10-04 23:45:00 0.00    NA
2005-10-05 11:45:00 0.02     2
2005-10-05 23:45:00 0.00    NA
2005-10-06 11:45:00 0.00    NA
2005-10-06 23:45:00 0.01     3
2005-10-07 11:45:00 0.00    NA
2005-10-07 23:45:00 0.00    NA
2005-10-08 11:45:00 0.00    NA
2005-10-08 23:45:00 0.16     4
2005-10-09 11:45:00 0.03     4
2005-10-09 23:45:00 0.00    NA
于 2013-04-30T23:15:08.987 回答