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)