## Scalar example
sjl <- list(sjl_m = lubridate::dhours(1.25),
sjl_e = lubridate::dhours(0.5),
sjl_n = lubridate::dhours(3))
n_w <- list(n_w_m = 3, n_w_e = 1, n_w_n = 4)
sjl_weighted(sjl, n_w)
#> [1] "7312.5s (~2.03 hours)" # Expected
sjl <- list(sjl_m = lubridate::dhours(1.25),
sjl_e = lubridate::as.duration(NA),
sjl_n = lubridate::dhours(3))
n_w <- list(n_w_m = 3, n_w_e = 1, n_w_n = 4)
sjl_weighted(sjl, n_w)
#> [1] NA # Expected
## Vector example
sjl <- list(sjl_m = c(lubridate::dhours(2), lubridate::dhours(2.45)),
sjl_e = c(lubridate::dhours(3.21), lubridate::as.duration(NA)),
sjl_n = c(lubridate::dhours(1.2), lubridate::dhours(5.32)))
n_w <- list(n_w_m = c(1, 3), n_w_e = c(4, 1), n_w_n = c(3, 3))
sjl_weighted(sjl, n_w)
#> [1] "8298s (~2.31 hours)" NA # Expected
## Checking the first output from vector example
if (requireNamespace("stats", quietly = TRUE)) {
i <- 1
x <- c(sjl[["sjl_m"]][i], sjl[["sjl_e"]][i], sjl[["sjl_n"]][i])
w <- c(n_w[["n_w_m"]][i], n_w[["n_w_e"]][i], n_w[["n_w_n"]][i])
lubridate::as.duration(stats::weighted.mean(x, w))
}
#> [1] "8298s (~2.31 hours)" # Expected
## Converting the output to hms
sjl <- list(sjl_m = lubridate::dhours(0.25),
sjl_e = lubridate::dhours(1.2),
sjl_n = lubridate::dhours(4.32))
n_w <- list(n_w_m = 4, n_w_e = 2, n_w_n = 1)
sjl_weighted(sjl, n_w)
#> [1] "3970.28571428571s (~1.1 hours)" # Expected
hms::as_hms(as.numeric(sjl_weighted(sjl, n_w)))
#> 01:06:10.285714 # Expected
## Rounding the output at the seconds level
round_time(sjl_weighted(sjl, n_w))
#> [1] "3970s (~1.1 hours)" # Expected
round_time(hms::as_hms(as.numeric(sjl_weighted(sjl, n_w))))
#> 01:06:10 # Expected
Run the code above in your browser using DataLab