Learn R Programming

eCerto (version 0.8.5)

calc_time_diff: Calculate time differences for suitable vectors.

Description

Calculation of a time difference between time points in a vector x and a specific start date d_start in month (days or years).

Usage

calc_time_diff(
  x = NULL,
  d_start = NULL,
  type = c("mon", "day", "year"),
  origin = "1900-01-01",
  exact = FALSE
)

Value

A numeric vector of length x containing calculated time differences in the unit specified by type. Not a difftime object.

Arguments

x

A vector of dates or character in format 'yyyy-mm-dd'.

d_start

A specific start date (if unspecified the minimum of x will be used to ensure positive values).

type

You may specify 'year' or 'day' instead of month here.

origin

The origin used.

exact

Function will return exact values instead of full month and year if this is set to TRUE.

Examples

Run this code
x <- c("2022-02-01", "2022-02-03", "2022-03-01", "2024-02-01")
calc_time_diff(x = x)
calc_time_diff(x = x, exact = TRUE)
calc_time_diff(x = x, type = "day")
calc_time_diff(x = x, type = "year")
calc_time_diff(x = x, type = "year", d_start = "2021-12-31")
calc_time_diff(x = 1:3, type = "day", origin = Sys.Date())

Run the code above in your browser using DataLab