Skip to content

Commit

Permalink
#2146 Fix Time Imputation Flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ahasoplakus committed Oct 26, 2023
1 parent dd8f8d9 commit 28ebe05
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 22 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,15 @@ importFrom(lubridate,date)
importFrom(lubridate,days)
importFrom(lubridate,duration)
importFrom(lubridate,floor_date)
importFrom(lubridate,hour)
importFrom(lubridate,hours)
importFrom(lubridate,is.Date)
importFrom(lubridate,is.POSIXct)
importFrom(lubridate,is.instant)
importFrom(lubridate,minute)
importFrom(lubridate,minutes)
importFrom(lubridate,rollback)
importFrom(lubridate,second)
importFrom(lubridate,time_length)
importFrom(lubridate,weeks)
importFrom(lubridate,years)
Expand All @@ -220,6 +223,7 @@ importFrom(purrr,keep)
importFrom(purrr,map)
importFrom(purrr,map2)
importFrom(purrr,map_chr)
importFrom(purrr,map_dbl)
importFrom(purrr,map_if)
importFrom(purrr,map_lgl)
importFrom(purrr,modify_at)
Expand Down
5 changes: 3 additions & 2 deletions R/admiral-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
#' warn as_data_mask list2
#' @importFrom utils capture.output str file.edit
#' @importFrom purrr map map2 map_chr map_lgl reduce walk keep map_if transpose
#' flatten every modify_at modify_if reduce compose pmap
#' flatten every modify_at modify_if reduce compose pmap map_dbl
#' @importFrom stringr str_c str_count str_detect str_extract str_glue
#' str_length str_locate str_locate_all str_match str_remove str_remove_all
#' str_replace str_replace_all str_split str_starts str_sub str_subset
#' str_trim str_to_lower str_to_title str_to_upper
#' @importFrom lubridate as_datetime ceiling_date date days duration floor_date is.Date is.instant
#' rollback time_length %--% ymd ymd_hms weeks years hours minutes is.POSIXct
#' rollback time_length %--% ymd ymd_hms weeks years hours minutes is.POSIXct hour
#' minute second
#' @importFrom tidyr crossing drop_na fill nest pivot_longer pivot_wider unnest
#' @importFrom tidyselect all_of any_of contains matches vars_select
#' @importFrom hms as_hms
Expand Down
31 changes: 23 additions & 8 deletions R/derive_date_vars.R
Original file line number Diff line number Diff line change
Expand Up @@ -1207,25 +1207,40 @@ compute_dtf <- function(dtc, dt) {
#' @export
#'
#' @examples
#' compute_tmf(dtc = "2019-07-18T15:25", dtm = as.POSIXct("2019-07-18T15:25:00"))
#' compute_tmf(dtc = "2019-07-18T15", dtm = as.POSIXct("2019-07-18T15:25:00"))
#' compute_tmf(dtc = "2019-07-18", dtm = as.POSIXct("2019-07-18"))
#' library(lubridate)
#'
#' compute_tmf(dtc = "2019-07-18T15:25", dtm = ymd_hms("2019-07-18T15:25:00"))
#' compute_tmf(dtc = "2019-07-18T15", dtm = ymd_hms("2019-07-18T15:25:00"))
#' compute_tmf(dtc = "2019-07-18", dtm = ymd("2019-07-18"))
#' compute_tmf(dtc = "2022-05--T00:00", dtm = ymd_hms("2022-05-15T23:59:59"))
#' compute_tmf(dtc = "2022-05--T23:00", dtm = ymd_hms("2022-05-15T23:59:59"))
#' compute_tmf(dtc = "2022-05--T23:59:00", dtm = ymd_hms("2022-05-15T23:59:59"))
compute_tmf <- function(dtc,
dtm,
ignore_seconds_flag = FALSE) {
assert_date_vector(dtm)
assert_character_vector(dtc)
assert_logical_scalar(ignore_seconds_flag)

valid_dtc <- is_valid_dtc(dtc)
warn_if_invalid_dtc(dtc, valid_dtc)

partial <- get_partialdatetime(dtc)
highest_miss <- convert_blanks_to_na(vector("character", length(dtc)))
for (c in c("hour", "minute", "second")) {
hms <- c("hour", "minute", "second")
time_part <-
map(set_names(hms), \(y) map_dbl(dtm, \(x) do.call(y, as.list(x))))
for (c in hms) {
highest_miss <-
if_else(is.na(partial[[c]]) & is.na(highest_miss), c, highest_miss)
if_else((is.na(partial[[c]]) & is.na(highest_miss)) |
(
!is.na(partial[[c]]) &
is.na(highest_miss) & as.numeric(partial[[c]]) != time_part[[c]]
),
c,
highest_miss
)
}
is_na <- is.na(dtm)
valid_dtc <- is_valid_dtc(dtc)
warn_if_invalid_dtc(dtc, valid_dtc)

map <- c(
hour = "H",
Expand Down
11 changes: 8 additions & 3 deletions man/compute_tmf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 18 additions & 9 deletions tests/testthat/test-derive_date_vars.R
Original file line number Diff line number Diff line change
Expand Up @@ -587,20 +587,26 @@ test_that("compute_tmf Test 30: compute TMF", {
"2003-12-15T-:15:18",
"2003-12-15T13:-:19",
"2020-07--T00:00",
"2020-07--T00:00:00"
"2020-07--T00:00:00",
"2022-05--T00:00",
"2022-05--T23:00",
"2022-05--T23:59:00"
)
input_dtm <- c(
as.POSIXct("2019-07-18T15:25:52"),
as.POSIXct("2019-07-18T15:25:00"),
as.POSIXct("2019-07-18T15:00:00"),
ymd_hms("2019-07-18T15:25:52"),
ymd_hms("2019-07-18T15:25:00"),
ymd_hms("2019-07-18T15:00:00"),
as.POSIXct("2019-07-18"),
as.POSIXct("2019-02-01"),
as.POSIXct(NA_character_),
as.POSIXct(NA_character_),
as.POSIXct("2003-12-15T23:15:18"),
as.POSIXct("2003-12-15T13:59:19"),
as.POSIXct("2020-07-31T00:00:59"),
as.POSIXct("2020-07-31T00:00:59")
ymd_hms("2003-12-15T23:15:18"),
ymd_hms("2003-12-15T13:59:19"),
ymd_hms("2020-07-31T00:00:59"),
ymd_hms("2020-07-31T00:00:00"),
ymd_hms("2022-05-15T23:59:59"),
ymd_hms("2022-05-15T23:59:59"),
ymd_hms("2022-05-15T23:59:59")
)
expected_output <- c(
NA_character_,
Expand All @@ -613,7 +619,10 @@ test_that("compute_tmf Test 30: compute TMF", {
"H",
"M",
"S",
NA_character_
NA_character_,
"H",
"M",
"S"
)

expect_equal(
Expand Down

0 comments on commit 28ebe05

Please sign in to comment.