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
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