确定有条件的季节长度(例如冬季雪季)

时间:2014-03-07 13:34:46

标签: r time-series

我想在以下数据框中确定雪季的长度:

DATE    SNOW
1998-11-01  0
1998-11-02  0
1998-11-03  0.9
1998-11-04  1
1998-11-05  0
1998-11-06  1
1998-11-07  0.6
1998-11-08  1
1998-11-09  2
1998-11-10  2
1998-11-11  2.5
1998-11-12  3
1998-11-13  6.5
1999-01-01  15
1999-01-02  15
1999-01-03  19
1999-01-04  18
1999-01-05  17
1999-01-06  17
1999-01-07  17
1999-01-08  17
1999-01-09  16
1999-03-01  6
1999-03-02  5
1999-03-03  5
1999-03-04  5
1999-03-05  5
1999-03-06  2
1999-03-07  2
1999-03-08  1.6
1999-03-09  1.2
1999-03-10  1
1999-03-11  0.6
1999-03-12  0
1999-03-13  1

雪季定义为雪深(SNOW)超过1厘米至少连续10天(所以如果11月有一天下雪,但是在它融化并且深度<1 cm后我们会考虑季节没开始)。

我的想法是确定:

1)积雪建立的日期(在我的例子中为1998-11-08)

2)“消失”的日期(此处为1999-03-11)

3)计算期间的长度(1998-11-05和1999-03-11之间的天数)

对于第3步,我可以使用this method轻松获取2个日期之间的数字。

但如何用条件定义日期?

1 个答案:

答案 0 :(得分:0)

这是一种方式:

# copy data from clipboard
d <- read.table(text=readClipboard(), header=TRUE)
# coerce DATE to Date type, add event grouping variable that numbers the groups
# sequentially and has NA for values not in events.
d <- transform(d, DATE=as.Date(DATE),
                  event=with(rle(d$SNOW >= 1), rep(replace(ave(values, values, FUN=seq), !values, NA), lengths)))
# aggregate event lengths in days
event.days <- aggregate(DATE ~ event, data=d, function(x) as.numeric(max(x) - min(x), units='days'))
# get those events greater than 10 days
subset(event.days, DATE > 10)
#   event DATE
# 3     3  122

您还可以使用事件分组变量来查找开始日期:

starts <- aggregate(DATE ~ event, data=d, FUN=head, 1)
# 1     1 1998-11-04
# 2     2 1998-11-06
# 3     3 1998-11-08
# 4     4 1999-03-13

然后将其与event.days合并:

merge(event.days, starts, by='event')
#   event DATE.x     DATE.y
# 1     1      0 1998-11-04
# 2     2      0 1998-11-06
# 3     3    122 1998-11-08
# 4     4      0 1999-03-13