Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

96 report time between in days #111

Merged
merged 3 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions R/process_event_data_t.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ process_event_data_t <- function(event_data, data_cutoff_dttm){
comment = NA, # remove for clarity (a comment against an event may not refer to the whole pivoted row)
aggregation = "none"
) |>
dplyr::filter(.data$event_date_or_datetime < data_cutoff_dttm) |> # remove events after the cutoff time (should only happen for retrospective reports)
dplyr::group_by(.data$ref) |>
dplyr::arrange(.data$event_date_or_datetime) |>

# add the theoretical "today" event to each group
dplyr::group_modify(~ tibble::add_row(.x, event_date_or_datetime = data_cutoff_dttm)) |>

# calculate the time between events, in days
dplyr::mutate(
time_between = .data$event_date_or_datetime - dplyr::lag(.data$event_date_or_datetime),
time_between = difftime(.data$event_date_or_datetime, dplyr::lag(.data$event_date_or_datetime), units = "days"),
time_between = as.integer(.data$time_between),
event_date_or_datetime = as.Date(.data$event_date_or_datetime)
) |>
dplyr::filter(!is.na(.data$time_between)) |>
dplyr::ungroup() |>
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-process_event_data_t.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"ref" = c(123, 123, 123),
"measure_name" = "Name",
"comment" = "comment",
"event_date_or_datetime" = as.Date(c("2020-01-01", "2020-01-03", "2020-01-13"))
"event_date_or_datetime" = as.POSIXct(c("2020-01-01", "2020-01-03", "2020-01-13"))
)

cutoff_dttm <- as.POSIXct("2020-01-31 23:59:59")
Expand All @@ -19,7 +19,7 @@
c("aggregation", "ref", "measure_name", "comment", "date", "value")
)

expect_equal(result[["date"]], as.Date(c("2020-01-03", "2020-01-13", "2020-01-31")))
expect_equal(result[["date"]], as.POSIXct(c("2020-01-03 00:00:00", "2020-01-13 00:00:00", "2020-01-31 23:59:59")))
expect_equal(result[["value"]], c(2, 10, 18))
expect_equal(result[["comment"]], c(NA, NA, NA)) # comments removed deliberately

Expand All @@ -32,8 +32,8 @@
"ref" = numeric(),
"measure_name" = character(),
"comment" = character(),
"event_date_or_datetime" = date()
)
"event_date_or_datetime" = lubridate::POSIXct()
)

expect_equal(
process_event_data_t(events),
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-spcr_make_data_bundle.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
expect_type(out[["target"]], "double")
expect_type(out[["allowable_days_lag"]], "integer")
expect_type(out[["measure_data"]], "list")
expect_s3_class(out[["last_date"]], "Date")
expect_s3_class(out[["last_date"]], "POSIXct")
expect_type(out[["updated_to"]], "character")
expect_type(out[["domain_heading"]], "logical")

Expand Down
Loading