我需要对降水数据进行子集化,并进行循环所需的计算,以使日期成为60天的窗口。
例如,在下面的代码中,我将数据设置为2013-08-15至2013-10-15。 对于下一次迭代,日期将是2013-10-16至2013-12-16,依此类推。...
# subset 2 months around flood
precip_boulder_AugOct <- boulder_daily_precip %>%
filter(DATE >= as.Date('2013-08-15') & DATE <= as.Date('2013-10-15'))
答案 0 :(得分:0)
如果我理解您的问题,这是解决问题的一种方法。您将需要弄弄日期:您实际上想要60天(包括60天),60天(不包括60天)还是2个月?
我创建了一个伪数据集来使用它,其中只有DATE
和val
列。
library(dplyr)
library(lubridate)
# Parameters
FIRST_INITIAL_DATE <- ymd("2013-08-15")
FINAL_INITIAL_DATE <- ymd("2014-08-15")
RANGE_DAYS <- 60
# Create vector of initial dates (before adding 60 days)
initial_dates <- seq(FIRST_INITIAL_DATE, FINAL_INITIAL_DATE, by = "days")
# Create vector of all dates (beginning with FIRST_INITIAL_DATE, ending with 60 + FINAL_INITIAL_DATE)
all_dates <- c(initial_dates, initial_dates[length(initial_dates)] + 1:RANGE_DAYS)
# Create fake data
boulder_daily_precip <- data.frame(
DATE = all_dates,
val = runif(n = length(all_dates))
)
# Obviously you need to change this loop. Currently `precip_boulder_AugOct` gets overwritten so this represents the last window.
for (date in initial_dates) {
precip_boulder_AugOct <- boulder_daily_precip %>%
filter(DATE >= date & DATE <= date + RANGE_DAYS)
}
precip_boulder_AugOct
## DATE val
## 1 2014-08-15 0.163611388
## 2 2014-08-16 0.816459793
## 3 2014-08-17 0.859669117
## ...
## 60 2014-10-13 0.514661876
## 61 2014-10-14 0.143665303
希望这会有所帮助!
答案 1 :(得分:0)
base R
解决方案
您开始设置开始日期
start <- as.Date('2013-08-15', origin='1970-01-01')
由于R无论如何都会将其视为double,因此您只需在其中添加一些内容即可。
(end <- start+60)
> [1] "2013-10-14"
您甚至可以使用它创建矢量
(timerange <- as.Date(start:end, origin='1970-01-01'))
[1] "2013-08-15" "2013-08-16" "2013-08-17" "2013-08-18" "2013-08-19" "2013-08-20" "2013-08-21" "2013-08-22" "2013-08-23" "2013-08-24" "2013-08-25" "2013-08-26"
[13] "2013-08-27" "2013-08-28" "2013-08-29" "2013-08-30" "2013-08-31" "2013-09-01" "2013-09-02" "2013-09-03" "2013-09-04" "2013-09-05" "2013-09-06" "2013-09-07"
[25] "2013-09-08" "2013-09-09" "2013-09-10" "2013-09-11" "2013-09-12" "2013-09-13" "2013-09-14" "2013-09-15" "2013-09-16" "2013-09-17" "2013-09-18" "2013-09-19"
[37] "2013-09-20" "2013-09-21" "2013-09-22" "2013-09-23" "2013-09-24" "2013-09-25" "2013-09-26" "2013-09-27" "2013-09-28" "2013-09-29" "2013-09-30" "2013-10-01"
[49] "2013-10-02" "2013-10-03" "2013-10-04" "2013-10-05" "2013-10-06" "2013-10-07" "2013-10-08" "2013-10-09" "2013-10-10" "2013-10-11" "2013-10-12" "2013-10-13"
[61] "2013-10-14"
现在您可以让
循环运行for(i in seq(0,1000,61)){
precip_boulder_AugOct <- boulder_daily_precip %>%
filter(DATE >= as.Date(start+i, origin='1970-01-01') & DATE <= as.Date(start+i+60, origin='1970-01-01'))
}
或任何您喜欢的循环