round_to_duration: Round a duration of days to other units

View source: R/datetime_tools.R

round_to_durationR Documentation

Round a duration of days to other units

Description

Convert a duration of days into approximate months or years, rounding it to a nearest value if desired. Note that time durations larger than the week are only approximate.

Usage

round_to_duration(num, from, to, nearest = NULL, dir = NULL)

Arguments

num

(Numeric/Integer) Time durations you want to convert.

from

(Character) The units of time that num is in, e.g. "days".

to

(Character) The units of time to convert to, e.g. "weeks".

nearest

(Numeric/Integer or NULL) The closest number to round the results to, e.g. 1 to round to integers. If NULL, no rounding is done.

dir

(Character or NULL) Controls the rounding function used if nearest is not NULL. If omitted or "both", the round() function is used. If "up", ceiling() is used. If "down", floor() is used.

Value

A numeric vector.

Authors

Examples

round_to_duration(134, "days", "weeks")
#> [1] 19.14286

# Note that this is the same as a naive calculation; if you were converting 
# days to months, then it's just days divided by 30.4375 (the average number 
# of days per month).
round_to_duration(134, "days", "months")
#> [1] 4.402464
134 / 30.4375
#> [1] 4.402464

# You can round the output too. This is useful if you want to express a time
# duration like, "1.5 months".
round_to_duration(134, "days", "weeks", nearest = 0.5, dir = "up")
#> [1] 19.5
round_to_duration(134, "days", "weeks", nearest = 0.5, dir = "down")
#> [1] 19


DesiQuintans/desiderata documentation built on April 9, 2023, 5:43 a.m.