R +最大流量+事件数的水文年份

时间:2018-07-09 09:24:20

标签: r date dataframe

我试图将水文年份分配给一系列日期和流量。我查了一下这个裁判:hydrological year time series,但这只是让我参与其中。 这是我的代码。数据文件已附加。

station <- read.table('57165.txt', sep=',')       
x <- station[, 4]
year <- station[, 1]
month <- station[, 2]
day <- station[, 3]
days <- as.Date(strptime(paste(station[, 3], station[, 2], station[, 1]), 
                         format="%d %m %Y"))  # create a date vector
df <- data.frame(date = days, flow = station[, 4])  # create data frame for dates and flows
df <- df[order(df$date), ]  # sort by date
lenYrs <- length(year(df$date[1]):year(df$date[length(df$date)]))
startYr <- format(df$date[1] - 365, "%Y")
startDate <- paste(startYr, '-09', '-01', sep='')
breaks <- seq(as.Date(startDate), length=lenYrs, by="year")
df$hydroYear <- cut(df$date, breaks, 
                    labels=year(breaks[1]):year(breaks[length(breaks) - 1]))

如上所述,我的目标是为数据框分配一个水文年份(即从9月1日开始,到8月31日结束(水文年份是9月))。接下来,我想提取每个水文年的事件数和每个水文年的最大流量。 上面代码的问题在于,它似乎不够健壮,并且错过了最近几年,并且我不确定这行:format(df$date[1]-365,"%Y")。最后,代码似乎并不优雅。 注意:有些年份没有数据,因此年份序列中缺少这些数据。预先感谢。

文件:(57165.txt)

  

1952,2,3,72 1952,5,14,33.08 1953,3,1,0.504 1954,2,20,2.291   1954,11,22,5.05 1954,12,18,0.472 1954,12,19,1.08 1955,12,24,14.14   1956,4,16,260 1957,3,22,15.2 1958,1,2,10.263 1958,11,5,3.665   1960,11,3,2.81 1960,12,6,6.19 1961,1,3,3.57 1961,10,27,58.75   1961,11,3,66.25 1961,12,14,3.475 1962,1,6,15.5 1963,4,9,6.67   1963,4,23,7.15 1963,10,10,9.433 1964,2,1,38.975 1965,1,11,27.38   1965,1,19,118 1965,10,6,28.56 1966,2,27,6.03 1966,4,4,17.745   1966,11,10,11.3 1967,5,15,338.4 1967,10,18,48.725 1967,11,10,4.11   1968,4,25,9.433 1968,5,25,12.8 1968,11,25,182.95 1969,5,13,​​7.772   1969,5,23,3 1970,11,2,114.33 1970,11,6,1155 1971,1,10,154   1971,9,18,29.15 1972,4,19,20.86 1972,11,25,62.5 1973,5,8,3.95   1974,1,20,1.77 1975,2,21,562 1976,3,12,182.95 1977,11,14,26.79   1977,12,14,47.75 1980,12,26,274 1981,9,26,3.285 1982,4,10,63.75   1982,10,23,2.905 1982,11,10,4.11 1983,12,25,22.64 1988,12,25,7.772   1989,2,22,10.885 1989,11,6,486.2 1990,10,20,53.6 1991,3,23,105.95   1993,12,22,776 1994,3,12,105.95 1994,10,5,3.095 1994,10,7,16.7   1994,10,12,12.5 1994,11,2,370.5 1995,2,14,34.46 1995,5,28,13.4   1996,2,8,148 1996,11,20,2.81 1997,1,23,2.81 1997,5,15,200.1   1997,10,19,134 1997,11,10,1.77 2000,10,15,71.5 2000,12,8,6.655   2000,12,9,19.18 2001,4,5,22.5 2001,5,2,14.3 2002,10,31,109.6   2003,1,22,5.345 2004,10,29,12.5 2004,11,20,2.24 2006,2,15,2.335   2007,1,22,4.26 2007,4,13,20.12 2008,2,19,5.665 2009,3,1,4.415   2009,12,8,9.76 2010,1,18,881.68 2010,2,16,14.746 2010,2,26,14.746   2011,4,4,1.366 2011,5,1,5.146 2012,9,30,35.07 2013,1,9,0.362   2013,2,1,4.076 2013,11,19,28.246 2014,3,10,50.1 2014,5,7,37.818   2014,12,12,8.414 2015,2,21,53.366 2015,9,15,913.1 2015,10,26,18.39   2016,3,26,17.8 2016,4,13,11.2 1976,1,1,0 1978,1,1,0 1979,1,1,0   1991,1,1,0 1992,1,1,0 1998,1,1,0 1999,1,1,0 2001,1,1,0 2003,1,1,0

0 个答案:

没有答案