R notebook dates, making dates to decades
I don't know of any package or function that facilitates "decades", though since we're working with integer years, it should be relatively straight-forward. Here are two base R methods with about the same performance.
set.seed(42)
dates <- as.Date("2000-01-01") + sample(10000, size = 5)
dates
# [1] "2006-06-27" "2014-06-09" "2025-06-08" "2003-06-06" "2024-03-01"
10 * (as.integer(format(dates, format = "%Y")) %/% 10)
# [1] 2000 2010 2020 2000 2020
10 * floor(as.integer(format(dates, format = "%Y")) / 10)
# [1] 2000 2010 2020 2000 2020
Edit: much faster method, in case performance is an issue:
10 * ((as.POSIXlt(dates)$year + 1900) %/% 10)
I believe this is faster because it does not convert to and from strings, so numeric ops are a bit faster. The POSIXlt
(as opposed to POSIXct
) storage of a timestamp bases the year on 1900:
Sys.Date()
# [1] "2022-01-17"
dput(as.POSIXlt(Sys.Date()))
# structure(list(sec = 0, min = 0L, hour = 0L, mday = 17L, mon = 0L,
# year = 122L, wday = 1L, yday = 16L, isdst = 0L), class = c("POSIXlt",
# "POSIXt"), tzone = "UTC")
which is fairly easy to work around.
Benchmark:
bench::mark(
intdiv = 10 * (as.integer(format(dates, format = "%Y")) %/% 10),
floor = 10 * floor(as.integer(format(dates, format = "%Y")) / 10),
posixlt = 10 * ((as.POSIXlt(dates)$year + 1900) %/% 10)
)
# # A tibble: 3 x 13
# expression min median `itr/sec` mem_alloc `gc/sec` n_itr n_gc total_time result memory time gc
# <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl> <int> <dbl> <bch:tm> <list> <list> <list> <list>
# 1 intdiv 16.2ms 19.9ms 48.1 978KB 2.00 24 1 499ms <dbl [10,000]> <Rprofmem [21 x 3]> <bench_tm [25]> <tibble [25 x 3]>
# 2 floor 16.2ms 21.5ms 48.0 978KB 2.09 23 1 480ms <dbl [10,000]> <Rprofmem [21 x 3]> <bench_tm [24]> <tibble [24 x 3]>
# 3 posixlt 740.2us 796.2us 941. 469KB 15.4 427 7 454ms <dbl [10,000]> <Rprofmem [10 x 3]> <bench_tm [434]> <tibble [434 x 3]>