从相同的日期和时间,不同的年份按时间序列填充NA r

时间:2018-09-28 09:37:05

标签: r time-series na

我有一个时间序列,其中包含三个不同年份的数据。有很多丢失的数据。我想用相同的日期和时间,但不同的年份来填充这些NA。

例如,如果在2017年缺少一个值,并且在2016年和2015年来自同一日期和时间的数据,我想对这两个值求平均值以填补2017年的丢失值。如果只有一个数据可用的一年,我想使用该单个数据点来替换缺少的值。

这是缺少值的df:

enter image description here

structure(list(timestamp = c("2015-09-26 06:30", "2016-09-26 06:30", 
"2017-09-26 06:30", "2015-09-26 07:00", "2017-09-26 07:00", "2015-09-26 07:30", 
"2016-09-26 07:30", "2017-09-26 07:30"), ex = c(NA, 5.52, NA, 
5.99, NA, 5.56, 5.24, NA), in = c(6.08, NA, NA, NA, NA, NA, 
NA, NA), nee = c(NA, -1.6965, NA, -3.4113, NA, -8.1687, -12.9374, 
NA), year = c(2015L, 2016L, 2017L, 2015L, 2017L, 2015L, 2016L, 
2017L), time = c("06:30", "06:30", "06:30", "07:00", "07:00", 
"07:30", "07:30", "07:30"), datetime = c("09-26 06:30", "09-26 06:30", 
"09-26 06:30", "09-26 07:00", "09-26 07:00", "09-26 07:30", "09-26 07:30", 
"09-26 07:30")), class = "data.frame", row.names = c(NA, -8L))

这是理想的df替代NA:

enter image description here

structure(list(timestamp = c("2015-09-26 06:30", "2016-09-26 06:30", 
"2017-09-26 06:30", "2015-09-26 07:00", "2017-09-26 07:00", "2015-09-26 07:30", 
"2016-09-26 07:30", "2017-09-26 07:30"), ex = c(NA, 5.52, NA, 
5.99, NA, 5.56, 5.24, NA), in = c(6.08, NA, NA, NA, NA, NA, 
NA, NA), nee = c(NA, -1.6965, NA, -3.4113, NA, -8.1687, -12.9374, 
NA), year = c(2015L, 2016L, 2017L, 2015L, 2017L, 2015L, 2016L, 
2017L), time = c("06:30", "06:30", "06:30", "07:00", "07:00", 
"07:30", "07:30", "07:30"), datetime = c("09-26 06:30", "09-26 06:30", 
"09-26 06:30", "09-26 07:00", "09-26 07:00", "09-26 07:30", "09-26 07:30", 
"09-26 07:30"), ex_filled = c(5.52, 5.52, 5.52, 5.99, 5.99, 5.56, 
5.24, 5.4), in_filled = c(6.08, 6.08, 6.08, NA, NA, NA, NA, NA
), nee_filled = c(-1.7, -1.7, -1.7, -3.41, -3.41, -8.17, -12.94, 
-10.55)), class = "data.frame", row.names = c(NA, -8L))

我尝试了一些for循环和使用Zoo的解决方案,但无法获得所需的东西。

0 个答案:

没有答案