gpt4 book ai didi

r - 插值时间序列

转载 作者:行者123 更新时间:2023-12-03 19:53:54 25 4
gpt4 key购买 nike

我有两组具有不同时间戳的数据。一组数据包含校准数据,另一组包含样本数据。校准的频率远低于 sample 。
我想做的是将校准数据(低频)插入到样本时间序列(高频)上。

sam <- textConnection("time, value
01:00:52, 256
01:03:02, 254
01:05:23, 255
01:07:42, 257
01:10:12, 256")

cal <- textConnection("time, value
01:01:02, 252.3
01:05:15, 249.8
01:10:02, 255.6")

sample <- read.csv(sam)

sample$time <- as.POSIXct(sample$time, format="%H:%M:%S")

calib <- read.csv(cal)

calib$time <- as.POSIXct(calib$time, format="%H:%M:%S")
(我看到的)最大的问题是数据的频率随机变化。
你们有没有人不得不做类似的事情?是否有一个 chron 或 zoo 函数可以满足我的要求(将低频数据插入到两个 ts 都是随机的高频数据上)?

最佳答案

您也可以使用approx像这样的功能,它会容易得多。只需确保您正在使用数据框即可。此外,使用 as.POSIXct 确保校准中的列格式和样本数据集中的格式相同。 .

 calib <- data.frame(calib); sample <- data.frame(sample)

IPcal <- data.frame(approx(calib$time,calib$value, xout = sample$time,
rule = 2, method = "linear", ties = mean))

head(IPcal)

# x y
#1 2017-03-22 01:00:52 252.3000
#2 2017-03-22 01:03:02 251.1142
#3 2017-03-22 01:05:23 249.9617
#4 2017-03-22 01:07:42 252.7707
#5 2017-03-22 01:10:12 255.6000

阅读更多关于 approxapproxfun documentation .

关于r - 插值时间序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13073686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com