如何根据R中的上一个日期和下一个日期填写缺失的数据? [英] How to fill missing data according to the date previous and next to it in R?
本文介绍了如何根据R中的上一个日期和下一个日期填写缺失的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有关此主题的另外两个问题:
解决方案
示例数据:
df<-data.frame(date = mdy_hm(c("10/10/2016 10:50","2016年10月12日12:07","2016年10月24日08:53")),图= c(5.73,NA_real_,6.09))
使用 zoo
软件包:
库(zoo)图书馆(magrittr)动物园(df $ figure,df $ date)%&%;%na.approx()%>%as.data.frame()
使用 lubridate
和 dplyr
库(dplyr)库(润滑)df%>%mutate(图= ifelse(is.na(图),滞后(图1)+(提前量(图1)-滞后(图1))*as.numeric(difftime(date,lag(date,1)))/as.numeric((difftime(lead(date(1,1),date)+ difftime(date,lag(date,1))))),图))%&%;%mutate(图=舍入(图,2))
Two more questions about this topic: A B
Take Fig.1 as an example, we can see that data in 10/12/2016 12:07 is missing. I want to use the previous and next row of data (i.e., 10/10/2016 10:50 5.73; 10/24/2016 08:53 6.09) to linear interpolate this missing data (not the mean value of "5.73" and "6.09", but according to the "date"). The example data file is attached below:
09/26/2016 11:57 5.42
10/10/2016 10:50 5.73
10/12/2016 12:07
10/24/2016 08:53 6.09
11/07/2016 11:25 6.43
11/21/2016 13:57 6.33
12/05/2016 14:01 7.97
12/19/2016 13:00 8.47
You can see Fig.2, we can use "Trend()" to attain this goal.
=TREND(M22:M23,L22:L23,O22)
I was wondering if there is a useful function as well in R?
解决方案
Example data:
df <- data.frame(date = mdy_hm(
c("10/10/2016 10:50",
"10/12/2016 12:07",
"10/24/2016 08:53")),
figure = c(5.73, NA_real_, 6.09))
Using the zoo
package:
library(zoo)
library(magrittr)
zoo(df$figure, df$date) %>%
na.approx() %>%
as.data.frame()
Using lubridate
and dplyr
library(dplyr)
library(lubridate)
df %>%
mutate(figure = ifelse(is.na(figure),
lag(figure, 1) + (lead(figure, 1) - lag(figure, 1)) *
as.numeric(difftime(date, lag(date, 1))) /
as.numeric((difftime(lead(date, 1), date) + difftime(date, lag(date, 1)))),
figure)) %>%
mutate(figure = round(figure, 2))
这篇关于如何根据R中的上一个日期和下一个日期填写缺失的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文