Replace NA date by the value of another colum

I have some missing values for a date column and to do calculations I need to replace them. I can recover the year from an another colum and I try to create a new variable with it :

year<-substr(final$idtype,0,4)
month<-10
day<-10
date<-paste0(year,month,day)
date<-as.Date(date, "%Y%m%d" )

Then, I want to use this column to replace only my missings values in my other column like this :

DateR<-as.Date(DateR,"%Y%m%d")
DateR[is.na(DateR)] <- date

but I have this warning message :

Warning message:
In NextMethod(.Generic) :
the number of objects to be replaced is not a multiple of the size of the replacement

My NA doesn't exist anymore but the replacement date doesn't match to the variable "date"

To illustrate I want to pass from this :
id type / dateR
20095030/2009-09-12
20106050/NA
20051211/2005-02-22

To this :
id type / dateR
20095030/2009-09-12
20106050/20101010
20051211/2005-02-22

But for now I have something like this and i don't understand why :
id type / dateR
20095030/2009-09-12
20106050/20131010
20051211/2005-02-22

I hope I was clear. Thank you for your time.

I do not know of any way to show numeric date as YYYYMMDD in a data frame or tibble. If the original dateR values are characters, you can build a string of the kind you want to replace the NA values. In the code below, I show one way to handle the NA replacement if the dateR values are numeric dates and another if they are characters.

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2

#If dateR is dates
DF <- tibble(id_type = c(20095467, 20081896, 20108956, 20113412),
                 dateR = as.Date(c("2009-03-15", "2008-11-12", NA, "2013-04-09")))
DF
#> # A tibble: 4 x 2
#>    id_type dateR     
#>      <dbl> <date>    
#> 1 20095467 2009-03-15
#> 2 20081896 2008-11-12
#> 3 20108956 NA        
#> 4 20113412 2013-04-09
MakeDate <- function(STR) {
  YEAR <- substr(STR, 1, 4)
  DateSTR <- paste(YEAR, "10", "10", sep = "-")
  as.Date(DateSTR)
}
DF <- DF |> mutate(dateR = ifelse(is.na(dateR), MakeDate(id_type), dateR),
                   dateR = as.Date(dateR, origin = "1970-01-01"))
DF
#> # A tibble: 4 x 2
#>    id_type dateR     
#>      <dbl> <date>    
#> 1 20095467 2009-03-15
#> 2 20081896 2008-11-12
#> 3 20108956 2010-10-10
#> 4 20113412 2013-04-09

#dateR is characters
DF <- tibble(id_type = c(20095467, 20081896, 20108956, 20113412),
             dateR = c("2009-03-15", "2008-11-12", NA, "2013-04-09"))
DF
#> # A tibble: 4 x 2
#>    id_type dateR     
#>      <dbl> <chr>     
#> 1 20095467 2009-03-15
#> 2 20081896 2008-11-12
#> 3 20108956 <NA>      
#> 4 20113412 2013-04-09
MakeDateString <- function(STR) {
  YEAR <- substr(STR, 1, 4)
  DateSTR <- paste0(YEAR, "10", "10")
}
DF <- DF |> mutate(dateR = ifelse(is.na(dateR), MakeDateString(id_type), dateR))
DF
#> # A tibble: 4 x 2
#>    id_type dateR     
#>      <dbl> <chr>     
#> 1 20095467 2009-03-15
#> 2 20081896 2008-11-12
#> 3 20108956 20101010  
#> 4 20113412 2013-04-09

Created on 2022-03-22 by the reprex package (v2.0.1)

At the origine, my variable are numeric and there are no NA but when I convert it into Date format NA appears where there is only the year :

dateR (as factor)
20090912
2013
20050222

dateR(as Date)
20090912
NA
20050222

Thank you for your answer, I will try this now

At risk of being a tidyverse tub-thumper:

DF <- DF %>% mutate(col1 = coalesce(col1, col2))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.