Replace NA´s in dates with another date

后端 未结 3 1876
遥遥无期
遥遥无期 2021-01-26 14:33

Data:

DB1 <- data.frame(orderItemID  = 1:10,     
orderDate = c(\"2013-01-21\",\"2013-03-31\",\"2013-04-12\",\"2013-06-01\",\"2014-01-01\", \"2014-02-19\",\"2         


        
3条回答
  •  隐瞒了意图╮
    2021-01-26 15:05

    First, convert the columns to Date objects:

    DB1[,2:3]<-lapply(DB1[,2:3],as.Date)
    

    Then, replace the NA elements:

    DB1$deliveryDate[is.na(DB1$deliveryDate)] <- 
           DB1$orderDate[is.na(DB1$deliveryDate)] +
           mean(difftime(DB1$orderDate,DB1$deliveryDate,units="days"),na.rm=TRUE)
    #   orderItemID  orderDate deliveryDate
    #1            1 2013-01-21   2013-01-23
    #2            2 2013-03-31   2013-03-01
    #3            3 2013-04-12   2013-04-14
    #4            4 2013-06-01   2013-06-04
    #5            5 2014-01-01   2014-01-03
    #6            6 2014-02-19   2014-02-21
    #7            7 2014-02-27   2014-02-28
    #8            8 2014-10-02   2014-10-04
    #9            9 2014-10-31   2014-11-01
    #10          10 2014-11-21   2014-11-23 
    

提交回复
热议问题