我有以下R矩阵:
Date MyVal
2016 1
2017 2
2018 3
....
2026 10
我想要做的是"吹嘘"所以它就像这样(月线值被线性插值):
Date MyVal
01/01/2016 1
02/01/2016 ..
....
01/01/2017 2
....
01/01/2026 10
我意识到我可以使用以下方法轻松生成序列:
DateVec <- seq(as.Date(paste(minYear,"/01/01", sep = "")), as.Date(paste(maxYear, "/01/01", sep = "")), by = "month")
我可以使用它来制作一个大矩阵,然后在DateVector上使用for循环填充内容,但我想知道是否有更优雅的R方式来做这个?
答案 0 :(得分:1)
您可以使用stats::approx
:
library(stats)
ipc <- approx(df$Date, df$MyVal, xout = DateVec,
rule = 1, method = "linear", ties = mean)
您可能需要先将原始数据框中的数据转换为月,日,并且格式为asPOSIXct
或as.Date
。
根据您提供的内容,这有效:
#reproducing the data-frame:
Date <- seq(2016,2026)
MyVal <- seq(1:11)
Date <- data.frame(as.Date(paste0(Date,"/01/01"))) #yyyy-mm-dd format
df <- cbind(Date, MyVal)
df <- as.data.frame(df)
colnames(df) <- c ("Date", "MyVal") #Changing Column Names
#Make the reference data-frame for interpolation:
minY <- min(df$Date, na.rm=T)
maxY <- max(df$Date, na.rm=T)
DateVec <- seq(minY, maxY, by = "month")
#Interpolation:
intrpltd_df <- approx(df$Date, df$MyVal, xout = DateVec,
rule = 1, method = "linear", ties = mean)
这是输出:
> head(data.frame(intrpltd_df))
# x y
# 1 2016-01-01 1.000000
# 2 2016-02-01 1.084699
# 3 2016-03-01 1.163934
# 4 2016-04-01 1.248634
# 5 2016-05-01 1.330601
# 6 2016-06-01 1.415301