【发布时间】:2016-12-17 19:39:41
【问题描述】:
我希望这是一个可以接受的 R/data.table 问题。
我有一个包含以下内容的 3 列表:
-
id地理位置 ID(303,453 个位置) -
month1990-2014 年超过 25 年的月份 -
spei一个在 -7 和 7 之间变化的气候指数。
我需要计算整个 1990-2014 年期间每个地点发生的干旱。干旱事件被定义为“SPEI 持续为负且 SPEI 达到 -1.0 或更低的时期。干旱在 SPEI 第一次低于零时开始,并以第一个正 SPEI 值结束。 -1.0 或更小的值”。
我知道使用 shift() 和滚动连接应该是可行的,但非常欢迎一些帮助!
# Sample table structure
dt <- data.table(
id = rep(1:303453, each=25*12),
month = rep(seq(as.Date("1990-01-01"), as.Date("2014-12-31"), "month"), 303453),
spei = runif(303453*25*12, -7, 7))
# A minimal example with 1 location over 12 months
library(data.table)
library(xts)
dt <- data.table(
id = rep("loc1", each=12),
month = seq(as.Date("2014-01-01"), as.Date("2014-12-31"), "month"),
spei = c(-2, -1.1, -0.5, 1.2, -1.2, 2.3, -1.7, -2.1, 0.9, 1.2, -0.9, -0.2))
spei.ts <- xts(dt$spei, order.by=dt$month, frequency="month")
plot(spei.ts, type="bars")
这显示了 1 年期间的 3 次干旱事件。这是我需要识别和计数的。
希望你们中的一些人更习惯于使用时间序列。 非常感谢,--Mel。
【问题讨论】:
-
请发布一个小的可重复示例和预期输出,以便于理解和交叉检查
-
我怀疑
rep(1:303453, each=25*12)可以被视为 small 可重现的例子
标签: r data.table time-series