vendredi 22 janvier 2021

Replace NA value in column with modified date in other column

I have the following dataset:

  A          B
2007-11-22 2004-11-18
      <NA> 2004-11-10

when the value of column A is NA, I want this value to be replaced by the date in B, except with an additional 25 days added. Here is what the outcome should look like:

   A          B
2007-11-22 2004-11-18
2004-12-05 2004-11-10

So far, I have tried the following if else formula, but with no success.

library(lubridate)
data$A<- ifelse(is.na(data$A),data$B+days(25),data$A)

Could anyone tell me what's wrong with it or give me an alternate solution? The code to build my dataset is below.

A<-c("2007-11-22 01:00:00", NA)
B<-c("2004-11-18","2004-11-10")
data<-data.frame(A,B)
data$A<-as.Date(data$A);data$B<-as.Date(data$B)

Aucun commentaire:

Enregistrer un commentaire