【发布时间】:2021-09-15 18:06:13
【问题描述】:
在 R (studio) 中,我尝试了很多次迭代,只将日期存储到 turning_point_dates 数据框中,但我只能让它存储循环数。我可以打印找到的每个日期,但还不能存储它们。
dates = data.frame(Date = seq(from = as.Date("2002-06-01"), to = as.Date("2011-09-30"), by = 'day'))
nums = c(98,99,100,101,102,103,104,105,106,107)
dataframe_of_numbers = data.frame(nums)
mat = matrix(ncol=0, nrow=0)
turning_point_dates = data.frame(mat)
for (i in 1:nrow(dataframe_of_numbers)){
print(dates$Date[dataframe_of_numbers[i,]])
turning_point_dates[i,] = dates$Date[dataframe_of_numbers[i,]]
}
turning_point_dates
我怎样才能将循环的实际日期存储到 turning_point_dates 数据框中?
turning_point_dates 输出如下所示的数据框: 说明:df [10 x 0]
1
2
3
4
5
6
7
8
9
10
1-10 of 10 rows
当我想要这样的数据框时:
"2002-09-06"
"2002-09-07"
"2002-09-08"
"2002-09-09"
"2002-09-10"
"2002-09-11"
"2002-09-12"
"2002-09-13"
"2002-09-14"
"2002-09-15"
【问题讨论】:
-
您能更清楚地了解您要做什么吗?此外,由于
as.Date("20011-09-31")引发错误,该代码不会按原样运行。 -
我进行了一些编辑以使其更加清晰,并修复了错误,以便它现在可以运行。谢谢。