View source: R/datetime_tools.R
round_to_duration | R Documentation |
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.
round_to_duration(num, from, to, nearest = NULL, dir = NULL)
num |
(Numeric/Integer) Time durations you want to convert. |
from |
(Character) The units of time that |
to |
(Character) The units of time to convert to, e.g. |
nearest |
(Numeric/Integer or |
dir |
(Character or |
A numeric vector.
Desi Quintans (http://www.desiquintans.com)
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.