下面提到的代码是否有循环?
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
,所以我需要循环,因为每次我的瓦尔日期增加,我都想添加一行新代码。
3条答案
按热度按时间nbewdwxp1#
我建议先创建一次日期序列,然后使用
sequence
和from =
创建索引号序列,然后根据该序列对日期进行子集化。oxf4rvwz2#
****递归**解决方案:
while
-回路:外出
fdx2calv3#
至于存储结果,您可以使用一个列表,然后使用
do.call
将其转换为 Dataframe 。另一个选择