【问题标题】:hydrological year time series水文年时间序列
【发布时间】:2014-02-27 16:04:10
【问题描述】:

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

数据如下:

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)

【问题讨论】:

    标签: r time


    【解决方案1】:

    将日期存储在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
    

    【讨论】:

      【解决方案2】:

      这里有一个单线来做到这一点。

      首先将日期转换为"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 
      

      【讨论】:

        【解决方案3】:

        试试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)
        

        【讨论】:

        • 非常感谢!!每个水文年(从 01/11 开始到明年 31/10)?如何选择计算年份的开始?
        • 可以使用window函数指定startend,见编辑。
        • 非常感谢您的迅速回复。我试过了,但是它仍然计算自然年(使用“窗口”函数使计算从给定日期开始,到当年的 12 月 31 日结束,下一年仍然从 1 月 1 日到 12 月 31 日计算)。我的目的是从第二年的 11 月 1 日到 10 月 31 日开始计算,并且不断地......例如最大值:1/11/2002 至 31/10/2003、1/11/2003 至 31/10/2004、1/11/2004 至 31/10/2005.....
        • @user3361298 -- 那么,有什么理由让你接受这个答案而不是我的答案,这有利于做你真正要求的事情?如果您只想要使用 xts 包的解决方案,您应该将其添加到您的问题中......
        • @Fernando 对不起,不是有意冒犯。我想我只是不明白您的回答如何帮助 OP 找到每个 水文 年的最大值。当我运行它时(即使有注释掉的位),它会将“2016-12-31”和“2017-02-25”都标识为最大值,即使这两个日期都属于同一个水文年份。也许我只是错过了一些东西,但我无法弄清楚它是什么......
        猜你喜欢
        • 1970-01-01
        • 2018-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-12
        • 2015-05-13
        • 2015-09-06
        • 2012-04-14
        相关资源
        最近更新 更多