NA instead of zero when subtracting dates with no data

Hello,

I am trying to generate code to create a numeric variable (to measure days, months, or years) by subtracting two dates (date of birth and date of consultation). However, the result assigns 0 (instead of NA) to records where one or both dates are NA. I would like those cases to be assigned NA instead.

I have been unable to find a solution online and would appreciate any help in identifying the issue.

Thank you in advance.

R

Hello,

It would help if you could provide a reproducible example (reprex) so we can what is going on. From my experience in R, arithmetic with NAs is typically sticky, i.e., when you apply some binary operation to one or more NAs, you get another NA.

Here is a quick reprex demonstrating this:

# load packages
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

# arithmetic with dates and NA values
as.Date(x = "2026-02-17") - NA
#> [1] NA
lubridate::today() - NA
#> [1] NA

Created on 2026-02-17 with reprex v2.1.1

Session info

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.5.1 (2025-06-13)
#>  os       macOS Sequoia 15.7.3
#>  system   x86_64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/Toronto
#>  date     2026-02-17
#>  pandoc   3.6.3 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/x86_64/ (via rmarkdown)
#>  quarto   1.7.31 @ /usr/local/bin/quarto
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.5   2025-04-23 [1] RSPM
#>  digest        0.6.39  2025-11-19 [1] RSPM
#>  evaluate      1.0.5   2025-08-27 [1] RSPM
#>  fastmap       1.2.0   2024-05-15 [1] RSPM
#>  fs            1.6.6   2025-04-12 [1] RSPM
#>  generics      0.1.4   2025-05-09 [1] RSPM
#>  glue          1.8.0   2024-09-30 [1] RSPM
#>  htmltools     0.5.9   2025-12-04 [1] RSPM
#>  knitr         1.51    2025-12-20 [1] RSPM
#>  lifecycle     1.0.5   2026-01-08 [1] RSPM
#>  lubridate   * 1.9.4   2024-12-08 [1] RSPM
#>  otel          0.2.0   2025-08-29 [1] RSPM
#>  reprex        2.1.1   2024-07-06 [1] RSPM
#>  rlang         1.1.7   2026-01-09 [1] RSPM
#>  rmarkdown     2.30    2025-09-28 [1] RSPM
#>  rstudioapi    0.18.0  2026-01-16 [1] RSPM
#>  sessioninfo   1.2.3   2025-02-05 [1] RSPM
#>  timechange    0.3.0   2024-01-18 [1] RSPM
#>  withr         3.0.2   2024-10-28 [1] RSPM
#>  xfun          0.56    2026-01-18 [1] RSPM
#>  yaml          2.3.12  2025-12-10 [1] RSPM
#> 
#>  [1] /Users/timothychisamore/Library/R/x86_64/4.5/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library
#>  * ── Packages attached to the search path.
#> 
#> ──────────────────────────────────────────────────────────────────────────────

All the best,

Tim

2 Likes

It would be helpful to see the exact code, as mentioned by @machupovirus, that is returning 0 when both or one date are NA , because subtracting dates in R should yield NA in that case.

Just to share… one approach I like using is: difftime() since it makes the time unit explicit.

library(dplyr)

df <- tibble(
  birth_date = as.Date(c("1990-01-01", NA, NA)),
  consultation_date = as.Date(c("2020-01-01", "2020-01-01", NA))
)

df %>%
  mutate(
    diff_days = difftime(consultation_date, birth_date, units = "days")
  )
# A tibble: 3 Γ— 3
  birth_date consultation_date diff_days 
  <date>     <date>            <drtn>    
1 1990-01-01 2020-01-01        10957 days
2 NA         2020-01-01           NA days
3 NA         NA                   NA days

you can change the units argument to weeks, months, years…

3 Likes

Hello!!

I was reviewing my code for creating the reprex and I found where the error was.

Thank you so much @machupovirus @Inielsen

2 Likes