Skip to contents

Calculate Number of Days Between Two Dates

Usage

age_days(df, start, end, colname = "age")

Arguments

df

<data.frame> containing date columns

start

column containing date(s) prior to end_date column

end

column containing date(s) after start_date column

colname

desired column name of output; default is "age"

Value

A tibble with a named column containing the calculated number of days.

Note

This calculation includes the end date in the sum (see example)

Examples

date_ex <- dplyr::tibble(
   x = seq.Date(
   as.Date("2021-01-01"),
   by = "month",
   length.out = 3),
   y = seq.Date(
   as.Date("2022-01-01"),
   by = "month",
   length.out = 3
   )
 )

age_days(df    = date_ex,
         start = x,
         end   = y)
#> # A tibble: 3 × 3
#>   x          y            age
#>   <date>     <date>     <dbl>
#> 1 2021-01-01 2022-01-01   366
#> 2 2021-02-01 2022-02-01   366
#> 3 2021-03-01 2022-03-01   366

date_ex |>
age_days(x, y, "days_between_x_y")
#> # A tibble: 3 × 3
#>   x          y          days_between_x_y
#>   <date>     <date>                <dbl>
#> 1 2021-01-01 2022-01-01              366
#> 2 2021-02-01 2022-02-01              366
#> 3 2021-03-01 2022-03-01              366

date_ex |>
age_days(start = x,
end = lubridate::today(),
colname = "days_since_x")
#> # A tibble: 3 × 3
#>   x          y          days_since_x
#>   <date>     <date>            <dbl>
#> 1 2021-01-01 2022-01-01         1384
#> 2 2021-02-01 2022-02-01         1353
#> 3 2021-03-01 2022-03-01         1325

date_ex |>
age_days(x, y, "days_between_x_y") |>
age_days(x, lubridate::today(), "days_since_x") |>
age_days(y, lubridate::today(), colname = "days_since_y")
#> # A tibble: 3 × 5
#>   x          y          days_between_x_y days_since_x days_since_y
#>   <date>     <date>                <dbl>        <dbl>        <dbl>
#> 1 2021-01-01 2022-01-01              366         1384         1019
#> 2 2021-02-01 2022-02-01              366         1353          988
#> 3 2021-03-01 2022-03-01              366         1325          960