r语言 - 水文年时间序列



目前我正在研究河流流量数据分析。我有从1935年到现在的每日出院记录。我想提取每个水位年份的年度最大排放量(从 01/11 开始到明年 31/10)。但是,我发现HydroTSM包只能处理自然年份。我尝试使用"动物园"包,但我发现很难计算,因为每年都有不同的日子。有人有想法吗?谢谢。

数据如下所示:

01-11-1935 663
02-11-1935 596
03-11-1935 450
04-11-1935 381
05-11-1935 354
06-11-1935 312

我的代码:

mydata<-read.table("discharge")
colnames(mydata) <- c("date","discharge")
library(zoo)
z<-zooreg(mydata[,2],start=as.Date("1935-11-1"))
mydta$date <- as.POSIXct(dat$date)
q.month<-daily2monthly(z,FUN=max,na.rm = TRUE,date.fmt = "%Y-%m-%d",out.fmt="numeric")
q.month.plain=coredata(q.month)
z.month<-zooreg(q.month.plain,start=1,frequency=12)

将日期存储在类 Date 的向量中,您可以只使用 cut()tapply() ,如下所示:

## Example data
df <- data.frame(date = seq(as.Date("1935-01-01"), length = 100, by = "week"),
                 flow = (runif(n = 100, min = 0, max = 1000)))
## Use vector of November 1st dates to cut data into hydro-years
breaks <- seq(as.Date("1934-11-01"), length=4, by="year")
df$hydroYear <- cut(df$date, breaks, labels=1935:1937)
## Find the maximum flow in each hydro-year
with(df, tapply(flow, hydroYear, max))
#     1935     1936     1937 
# 984.7327 951.0440 727.4210 

## Note: whenever using `cut()`, I take care to double-check that 
## I've got the cuts exactly right
cut(as.Date(c("1935-10-31", "1935-11-01")), breaks, labels=1935:1937)
# [1] 1935 1936
# Levels: 1935 1936 1937

这里有一行话来做到这一点。

首先将日期转换为类"yearmon"。此类将年月表示为一年之和的整数部分,将月份表示为小数部分(Jan = 0,Feb = 1/12,依此类推)。 添加 2/12 以将 11 月转换为 1 月,然后截断以仅给出年份。 聚合这些。 尽管我们使用的测试数据从水电年年初开始,但即使数据不是在水电年开始时开始,此解决方案也有效。

# test data
library(zoo)
z <- zooreg(1:1000, as.Date("2000-11-01")) # test input
aggregate(z, as.integer(as.yearmon(time(z)) + 2/12), max)

这给出了:

2001 2002 2003 
 365  730 1000 

尝试xts包,它与zoo一起工作:

require(zoo)    
require(xts)
dates = seq(Sys.Date(), by = 'day', length = 365 * 3)
y = cumsum(rnorm(365 * 3))    
serie = zoo(y, dates)
# if you need to specify `start` and `end`
# serie = window(serie, start = "2015-06-01")
# xts function
apply.yearly(serie, FUN = max)

最新更新