简体   繁体   English

线性插值R

[英]Linear interpolation R

I have this data.frame (12x2)called df_1 which represents monthly values : 我有一个名为df_1的data.frame (12x2),它代表月度值:

      month    df_test
 [1,]    1 -1.4408567
 [2,]    2 -1.0007642
 [3,]    3  2.1454113
 [4,]    4  1.6935537
 [5,]    5  0.1149219
 [6,]    6 -1.3205144
 [7,]    7  1.0277486
 [8,]    8  1.0323482
 [9,]    9 -0.1442319
[10,]   10 -0.2091197
[11,]   11 -0.6803158
[12,]   12  0.5965196

and this data.frame (8760x2) called df_2 where each rows represent a value associated to an interval of one hour of a day. 以及称为df_2的data.frame (8760x2),其中每一行代表与一天的一小时间隔相关的值。 This data.frame contains hourly values for one year: data.frame包含一年的每小时值:

                   time           df_time
1           2015-01-01 00:00:00 -0.4035650
2           2015-01-01 01:00:00  0.1800579
3           2015-01-01 02:00:00 -0.3770589
4           2015-01-01 03:00:00  0.2573456
5           2015-01-01 04:00:00  1.2000178
6           2015-01-01 05:00:00 -0.4276127
...........................................
                  time                df_time
8755           2015-12-31 18:00:00  1.3540119
8756           2015-12-31 19:00:00  0.4852843
8757           2015-12-31 20:00:00 -0.9194670
8758           2015-12-31 21:00:00 -1.0751814
8759           2015-12-31 22:00:00  1.0097749
8760           2015-12-31 23:00:00 -0.1032468

I want to obtain df_1 for each hour of each day. 我想每天获取每天的df_1。 The problem is that all months do not have the same amount of days. 问题在于,所有月份的天数都不相同。

Finally we should obtain a data.frame called df_3 (8760x2) that has interpolated values between the values of df_1. 最后,我们应该得到一个data.frame称为df_3(8760x2)已经df_1的值之间的插值。

Thanks for help! 感谢帮助!

Here's done with zoo . 这是zoo I'm assuming that the monthly value is associated with a specific datetime stamp (middle of the month, midnight) - you have to do that. 我假设每月值与特定的日期时间戳记(月中,午夜)相关联-您必须这样做。 If you want a different datetime stamp, just change the value. 如果要使用其他日期时间戳,只需更改该值即可。

library(zoo)
library(dplyr)
library(tidyr)

df_3 <- df_1 %>%
   mutate(time = paste(2015, month, "15 00:00:00", sep = "-"),
          time = as.POSIXct(strptime(time, "%Y-%m-%d %H:%M:%S"))) %>%
   full_join(df_2) %>%
   arrange(time) %>%
   mutate(df_test = na.approx(df_test, rule = 2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM