添加缺失的行但不会在日期更改时添加

时间:2017-01-23 13:50:21

标签: r dplyr

我正在尝试向数据帧添加缺失的行(在NO_REF的每个值内),同时在某些列上进行线性插值,并在其他列上插入最后的非NA值。当间隙之后的DATE_X值大于间隙之前的最后一个DATE值时,我无法弄清楚如何防止插入缺少的日期。

这是我的数据框:

df = data.frame(DATE = as.Date(c("2016-01-31","2016-03-31","2016-05-31","2016-08-31","2016-12-31","2016-02-29","2016-04-30","2016-06-30","2016-08-31","2016-10-31","2016-12-31","2015-01-31","2015-02-28","2015-06-30","2015-10-31","2015-12-31")), 
            DATE_X = as.Date(c("2010-01-31","2010-01-31","2016-04-30","2015-03-31","2015-03-31","2010-10-31","2010-10-31","2016-05-31","2016-05-31","2015-07-31","2015-07-31","2013-01-31","2013-01-31","2013-01-31","2015-09-30","2015-09-30")),
            NO_REF = c("P1","P1","P1","P2","P2","O1","O1","O1","O1","R1","R2","Q1","Q1","Q1","Q1","Q1"),
            KAP = as.double(15:30),
            DIV =c("PI","PI","PI","PI","PI","OP","OP","OP","OP","PR","PR","OP","OP","OP","OP","OP"))

这是我的代码:

library(dplyr)
library(multidplyr)
library(zoo)

cluster <- create_cluster(3)
cluster_eval(cluster,library(dplyr))
cluster_eval(cluster,library(zoo))

result = df %>% partition(NO_REF,cluster=cluster) %>%
group_by(NO_REF) %>%
do(left_join(data.frame(NO_REF = .$NO_REF[1], DATE = seq(min(.$DATE)+1, max(.$DATE)+1, by="1 month")-1), ., 
           by=c("NO_REF","DATE"))) %>%  mutate(DATE_X=na.locf(DATE_X, na.rm=FALSE),
             DIV=na.locf(DIV, na.rm=FALSE), KAP=na.approx(KAP)) %>% collect()

在下表中,蓝色行不应该在最终结果中。

预期结果:

enter image description here

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

这可能不是最有效的方法,但我认为它可以做到你想要的:

library(dplyr)
library(multidplyr)
library(zoo)

cluster <- create_cluster(3)
cluster_eval(cluster,library(dplyr))
cluster_eval(cluster,library(zoo))

result = df %>% partition(NO_REF,cluster=cluster) %>%
    group_by(NO_REF) %>%
    do(left_join(data.frame(NO_REF = .$NO_REF[1], DATE = seq(min(.$DATE)+1, max(.$DATE)+1, by="1 month")-1), ., 
       by=c("NO_REF","DATE"))) %>%  
    filter(!(is.na(DATE_X) & 
             na.locf(DATE_X, fromLast=TRUE, na.rm=FALSE)>
             na.locf(DATE+days(ifelse(is.na(DATE_X), NA, 0)), na.rm=FALSE))) %>% 
    mutate(DATE_X=na.locf(DATE_X, na.rm=FALSE),
           DIV=na.locf(DIV, na.rm=FALSE), 
           KAP=na.approx(KAP)) %>% 
    collect()

简而言之,DATE列被视为NA,并在缺少DATE_X,向后传送DATE_X,后者大于前者的行中继续前进DATE_X { <1}}丢失了。