基于r中的日期插值数据帧

基于r中的日期插值数据帧,r,date,dataframe,interpolation,R,Date,Dataframe,Interpolation,我想优化我的程序,根据日期序列插值数据帧列。 事情是这样的。我有一个包含两列的数据框,例如 d <- data.frame(x=c("2012-01-01","2013-01-01","2014-01-01","2015-01-01","2016-01-01","2017-01-01","2018-01-01","2019-01-01"), y=c(70, 80, 90, 80, 75 ,70, 75, 60)) d检查一下。谢谢!我试过了,但我想我不知道

我想优化我的程序,根据日期序列插值数据帧列。 事情是这样的。我有一个包含两列的数据框,例如

d <- data.frame(x=c("2012-01-01","2013-01-01","2014-01-01","2015-01-01","2016-01-01","2017-01-01","2018-01-01","2019-01-01"),
                y=c(70, 80, 90, 80, 75 ,70, 75, 60))

d检查一下。谢谢!我试过了,但我想我不知道如何使用它。我键入:
f
xx <- seq(as.Date(x[1]),as.Date(x[length(x)]),by="months")
# initialize the final data frame
r <- data.frame(matrix(NA,length(xx),2))
# copy the data I know
for (i in 1:length(x))
  r[which(xx==as.Date(d$x[i])),] <- d[i,]
# set the date sequence on the first column
r[,1] <- as.character(xx,"%Y/%m/%d")
# interpolate the values on the second column
r[,2] <- as.numeric(na.approx(r[,2]))