提问人:Zohaib 提问时间:3/14/2023 最后编辑:Darren TsaiZohaib 更新时间:3/16/2023 访问量:133
日期序列中的 While 循环
While loop in Date sequence
问:
下面提到的代码是否有任何循环?
library(lubridate)
Val_Date <- as.Date("2022-01-1", format = "%Y-%m-%d")
Inception_Date <- as.Date("2012-01-01", format = "%Y-%m-%d")
a <- data.frame(Date = seq(Inception_Date, Val_Date, by = "years"))
b <- data.frame(Date = seq(Inception_Date + years(1), Val_Date, by = "years"))
c <- data.frame(Date = seq(Inception_Date + years(2), Val_Date, by = "years"))
d <- data.frame(Date = seq(Inception_Date + years(3), Val_Date, by = "years"))
e <- data.frame(Date = seq(Inception_Date + years(4), Val_Date, by = "years"))
f <- data.frame(Date = seq(Inception_Date + years(5), Val_Date, by = "years"))
g <- data.frame(Date = seq(Inception_Date + years(6), Val_Date, by = "years"))
h <- data.frame(Date = seq(Inception_Date + years(7), Val_Date, by = "years"))
i <- data.frame(Date = seq(Inception_Date + years(8), Val_Date, by = "years"))
j <- data.frame(Date = seq(Inception_Date + years(9), Val_Date, by = "years"))
k <- data.frame(Date = seq(Inception_Date + years(10), Val_Date, by = "years"))
complete <- rbind(a, b, c, d, e, f, g, h, i, j, k)
在我的中等于 ,所以我需要循环,因为每次我的值日期增加时,我都想添加一行新的代码。k
Inception_Date + year(10)
val_Date
答:
0赞
dandrews
3/14/2023
#1
for (i in unique(a)) {
print(i)
# Edit to adrees OP's comment
x=as.Date(a[i,1])
seq(x,length=2,by='1 year')[2]
}
至于存储结果,您可以使用列表,然后将其制作成数据帧。do.call
mylist <- list()
for (i in 1:length(a$Date)) {
mylist[[i]]=a[i,1]
}
do.call('rbind',mylist)
另一种选择
for (i in seq.Date(from = Inception_Date,to=Val_Date,by='year')) {
print(i)
# do other stuff
}
评论
0赞
dandrews
3/14/2023
没关系,只需在这两个循环中的任何一个中添加一个调用来提前日期,它就会在每次迭代中自动这样做。我只是使用了这些简单的例子,因为你没有提供足够的信息让我确切地知道你想要实现什么。
0赞
dandrews
3/14/2023
我为一个选项在第一个循环中添加了两行,请参阅此处 stackoverflow.com/questions/3312964/how-to-subtract-years
0赞
dandrews
3/15/2023
请注意,OP 删除了他们的评论并更新了问题,因此这些答案现在可能超出了新问题的范围。
1赞
Darren Tsai
3/14/2023
#2
- 递归解决方案:
date_seq_recur <- function(start, end) {
if(start <= end)
c(seq(start, end, by = "year"), date_seq_recur(start + years(1), end))
}
date_seq_recur(Inception_Date, Val_Date)
while
-圈:
date_seq_while <- function(start, end) {
date <- as.Date(numeric())
while(start <= end) {
date <- c(date, seq(start, end, by = "year"))
start <- start + years(1)
}
return(date)
}
date_seq_while(Inception_Date, Val_Date)
外出
# [1] "2012-01-01" "2013-01-01" "2014-01-01" "2015-01-01" "2016-01-01" "2017-01-01"
# [7] "2018-01-01" "2019-01-01" "2020-01-01" "2021-01-01" "2022-01-01" "2013-01-01"
# [13] "2014-01-01" "2015-01-01" "2016-01-01" "2017-01-01" "2018-01-01" "2019-01-01"
# [19] "2020-01-01" "2021-01-01" "2022-01-01" "2014-01-01" "2015-01-01" "2016-01-01"
# [25] "2017-01-01" "2018-01-01" "2019-01-01" "2020-01-01" "2021-01-01" "2022-01-01"
# [31] "2015-01-01" "2016-01-01" "2017-01-01" "2018-01-01" "2019-01-01" "2020-01-01"
# [37] "2021-01-01" "2022-01-01" "2016-01-01" "2017-01-01" "2018-01-01" "2019-01-01"
# [43] "2020-01-01" "2021-01-01" "2022-01-01" "2017-01-01" "2018-01-01" "2019-01-01"
# [49] "2020-01-01" "2021-01-01" "2022-01-01" "2018-01-01" "2019-01-01" "2020-01-01"
# [55] "2021-01-01" "2022-01-01" "2019-01-01" "2020-01-01" "2021-01-01" "2022-01-01"
# [61] "2020-01-01" "2021-01-01" "2022-01-01" "2021-01-01" "2022-01-01" "2022-01-01"
评论
0赞
Zohaib
3/14/2023
如果我想再添加一列,重复 (length(seq(Inception_Date, Val_Date, by = “years”)) 乘以 Inception_Date 年。
0赞
Zohaib
3/14/2023
例如,这样的代码 Int_Yr_0 = length(seq(as.日期(Inception_Date),作为。Date(Val_Date), by =“years”)) a<- data.frame(Int_Yr = seq(as.日期(Inception_Date),作为。Date(Val_Date), by =“years”), Loss_Yr = rep(as.numeric(format(Inception_Date,'%Y'),Int_Yr_0))) 像这样有第二列loss_year
0赞
Darren Tsai
3/14/2023
@Zohaibdates <- seq(Inception_Date, Val_Date, by = "years")
; rep(format(dates, '%Y'), rev(seq_along(dates)))
2赞
Merijn van Tilborg
3/14/2023
#3
我建议创建一次日期序列,然后使用 with 创建索引号序列,然后根据该序列子集日期。sequence
from =
dates <- seq(as.Date("2012-01-1"),as.Date("2022-01-1"), by ="years")
n <- length(dates)
dates[sequence(n:1, from = 1:n)]
# [1] "2012-01-01" "2013-01-01" "2014-01-01" "2015-01-01" "2016-01-01" "2017-01-01" "2018-01-01" "2019-01-01" "2020-01-01" "2021-01-01" "2022-01-01" "2013-01-01"
# [13] "2014-01-01" "2015-01-01" "2016-01-01" "2017-01-01" "2018-01-01" "2019-01-01" "2020-01-01" "2021-01-01" "2022-01-01" "2014-01-01" "2015-01-01" "2016-01-01"
# [25] "2017-01-01" "2018-01-01" "2019-01-01" "2020-01-01" "2021-01-01" "2022-01-01" "2015-01-01" "2016-01-01" "2017-01-01" "2018-01-01" "2019-01-01" "2020-01-01"
# [37] "2021-01-01" "2022-01-01" "2016-01-01" "2017-01-01" "2018-01-01" "2019-01-01" "2020-01-01" "2021-01-01" "2022-01-01" "2017-01-01" "2018-01-01" "2019-01-01"
# [49] "2020-01-01" "2021-01-01" "2022-01-01" "2018-01-01" "2019-01-01" "2020-01-01" "2021-01-01" "2022-01-01" "2019-01-01" "2020-01-01" "2021-01-01" "2022-01-01"
# [61] "2020-01-01" "2021-01-01" "2022-01-01" "2021-01-01" "2022-01-01" "2022-01-01"
0赞
Nick Chr
3/16/2023
#4
使用 from timeplyr,这是一个矢量化时间序列函数,可以处理润滑周期/持续时间以及非整数增量。time_seq_v()
# remotes::install_github("NicChr/timeplyr")
library(timeplyr)
library(lubridate)
Val_Date <- as.Date("2022-01-1", format = "%Y-%m-%d")
Inception_Date <- as.Date("2012-01-01", format = "%Y-%m-%d")
time_seq_v(Inception_Date + years(0:10), Val_Date, units = "years", num = 1)
#> [1] "2012-01-01" "2013-01-01" "2014-01-01" "2015-01-01" "2016-01-01"
#> [6] "2017-01-01" "2018-01-01" "2019-01-01" "2020-01-01" "2021-01-01"
#> [11] "2022-01-01" "2013-01-01" "2014-01-01" "2015-01-01" "2016-01-01"
#> [16] "2017-01-01" "2018-01-01" "2019-01-01" "2020-01-01" "2021-01-01"
#> [21] "2022-01-01" "2014-01-01" "2015-01-01" "2016-01-01" "2017-01-01"
#> [26] "2018-01-01" "2019-01-01" "2020-01-01" "2021-01-01" "2022-01-01"
#> [31] "2015-01-01" "2016-01-01" "2017-01-01" "2018-01-01" "2019-01-01"
#> [36] "2020-01-01" "2021-01-01" "2022-01-01" "2016-01-01" "2017-01-01"
#> [41] "2018-01-01" "2019-01-01" "2020-01-01" "2021-01-01" "2022-01-01"
#> [46] "2017-01-01" "2018-01-01" "2019-01-01" "2020-01-01" "2021-01-01"
#> [51] "2022-01-01" "2018-01-01" "2019-01-01" "2020-01-01" "2021-01-01"
#> [56] "2022-01-01" "2019-01-01" "2020-01-01" "2021-01-01" "2022-01-01"
#> [61] "2020-01-01" "2021-01-01" "2022-01-01" "2021-01-01" "2022-01-01"
#> [66] "2022-01-01"
创建于 2023-03-16 使用 reprex v2.0.2
评论
for(currentdate in a){print(currentdate)}