Replace NA´s in dates with another date

后端 未结 3 1877
遥遥无期
遥遥无期 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 
    
    0 讨论(0)
  • 2021-01-26 15:11

    You can do:

    DB1 =cbind(DB1$orderItemID,as.data.frame(lapply(DB1[-1], as.character)))
    
    days = round(mean(DB1$deliveryDate-DB1$orderDate, na.rm=T))
    mask = is.na(DB1$deliveryDate)
    
    DB1$deliveryDate[mask] = DB1$orderDate[mask]+days
    
    #   DB1$orderItemID  orderDate deliveryDate
    #1                1 2013-01-21   2013-01-23
    #2                2 2013-03-31   2013-04-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
    

    I re-arrange your data since they were not clean:

    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","2014-02-27","2014-10-02","2014-10-31","2014-11-21"),  
    deliveryDate = c("2013-01-23", "2013-04-01", NA, "2013-06-04", "2014-01-03", NA, "2014-02-28", "2014-10-04", "2014-11-01", "2014-11-23"))
    
    0 讨论(0)
  • 2021-01-26 15:26

    Assuming that you have entered your data like this (note that NAs are not enclosed in quotes so they are read as NAs and not "NA")...

    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","2014-02-27","2014-10-02","2014-10-31","2014-11-21"),  
      deliveryDate = c("2013-01-23", "2013-03-01", NA, "2013-06-04", "2014-01-03", NA, "2014-02-28", "2014-10-04", "2014-11-01", "2014-11-23"),
      stringsAsFactors = FALSE)
    

    ...and, per Nicola's answer, done this to get the formatting right...

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

    ...this also works:

    library(lubridate)
    DB1$deliveryDate <- with(DB1, as.Date(ifelse(is.na(deliveryDate), orderDate + days(2), deliveryDate), origin = "1970-01-01"))
    

    Or you could use dplyr and pipe it:

    library(lubridate)
    library(dplyr)
    DB2 <- DB1 %>%
      mutate(deliveryDate = ifelse(is.na(deliveryDate), orderDate + days(2), deliveryDate)) %>%
      mutate(deliveryDate = as.Date(.[,"deliveryDate"], origin = "1970-01-01"))
    
    0 讨论(0)
提交回复
热议问题