R语言 日期序列中的While循环

zynd9foi  于 2023-03-15  发布在  其他
关注(0)|答案(3)|浏览(106)

下面提到的代码是否有循环?

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,所以我需要循环,因为每次我的瓦尔日期增加,我都想添加一行新代码。

nbewdwxp

nbewdwxp1#

我建议先创建一次日期序列,然后使用sequencefrom =创建索引号序列,然后根据该序列对日期进行子集化。

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"
oxf4rvwz

oxf4rvwz2#

****递归**解决方案:

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"
fdx2calv

fdx2calv3#

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将其转换为 Dataframe 。

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
}

相关问题