使用 R 中的线性插值将整个 DataFrame 缩小到高时间分辨率
我有一个包含许多列的数据框。数据的时间分辨率为 3 小时。我想通过现有 DataFrame 的线性插值以 30 分钟的间隔创建一个新的 DataFrame。我知道一种基于 approxfun
的方法,但我不知道如何将其应用于整个 DataFrame。有没有更有效的方法来解决这个问题?下面是示例 DataFrame 和我尝试的方法:
df <- structure(list(DateTime = structure(c(1475285400, 1475296200, 1475307000, 1475317800, 1475328600, 1475339400, 1475350200, 1475361000, 1475371800, 1475382600, 1475393400, 1475404200, 1475415000, 1475425800, 1475436600, 1475447400, 1475458200, 1475469000, 1475479800, 1475490600 ), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
X1 = c(294.3, 292.5, 291, 289.7, 289.6, 294.5, 297.5, 298.4, 296.3, 292.9, 290.9, 289.8, 290, 296.3, 297.9, 299.2, 297.1, 293.7, 291.7, 290),
X2 = c(0.16, 0.16, 0.16, 0.16, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.15, 0.15)), row.names = c(NA, 20L), class = "data.frame")
DateTimes <- seq(from=as.POSIXct("2016-10-01 00:00:00",tz="UTC"),to=as.POSIXct("2016-10-04 23:30:00",tz="UTC"),by="30 min")
ApproxFun <- approxfun(x = as.POSIXct(df$DateTime), y = df$X1)
X1_linear_filled <- ApproxFun(DateTimes)
但上述方法的问题是我必须对所有列重复该过程。
I have a DataFrame with many columns. The data is at 3 hour temporal resolution. I want to create a new DataFrame at 30 minutes interval by linear interpolation from the existing DataFrame. I know one approach based on approxfun
but I don't how to apply this for the entire DataFrame. Is there any more efficient approach for this problem? Below is the sample DataFrame and the approach I tried:
df <- structure(list(DateTime = structure(c(1475285400, 1475296200, 1475307000, 1475317800, 1475328600, 1475339400, 1475350200, 1475361000, 1475371800, 1475382600, 1475393400, 1475404200, 1475415000, 1475425800, 1475436600, 1475447400, 1475458200, 1475469000, 1475479800, 1475490600 ), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
X1 = c(294.3, 292.5, 291, 289.7, 289.6, 294.5, 297.5, 298.4, 296.3, 292.9, 290.9, 289.8, 290, 296.3, 297.9, 299.2, 297.1, 293.7, 291.7, 290),
X2 = c(0.16, 0.16, 0.16, 0.16, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.15, 0.15)), row.names = c(NA, 20L), class = "data.frame")
DateTimes <- seq(from=as.POSIXct("2016-10-01 00:00:00",tz="UTC"),to=as.POSIXct("2016-10-04 23:30:00",tz="UTC"),by="30 min")
ApproxFun <- approxfun(x = as.POSIXct(df$DateTime), y = df$X1)
X1_linear_filled <- ApproxFun(DateTimes)
But the problem with the above approach is that I have to repeat the procedure for all the columns.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对感兴趣的列进行
lapply
怎么样?或使用
summarize(across())
输出:
What about
lapply
over the columns of interest?or with
summarize(across())
Output: