Skip to content

Commit

Permalink
down to 2 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
joethorley committed Jan 22, 2025
1 parent f0f6867 commit b405e61
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
8 changes: 4 additions & 4 deletions R/classify-time-series-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ check_time_series_args <- function(data,
chk::chk_gte(gap_range)
}


set_status_id <- function(data) {
data |>
duckplyr::mutate(
Expand Down Expand Up @@ -215,23 +214,24 @@ classify_time_series_data <- function(data,

data <- data |>
dplyr::left_join(questionable_range, by = dplyr::join_by(closest(x$.date_time >= y$.start_date_time))) |>
duckplyr::mutate(status_id = duckplyr::if_else(.data$.date_time <= .data$.end_date_time, 2L, .data$status_id, .data$status_id)) |>
duckplyr::mutate(status_id = duckplyr::if_else(.data$status_id == 1L & .data$.date_time <= .data$.end_date_time, 2L, .data$status_id, .data$status_id)) |>
duckplyr::select(!c(".start_date_time", ".end_date_time")) |>
dplyr::left_join(erroneous_range, by = dplyr::join_by(closest(x$.date_time >= y$.start_date_time))) |>
duckplyr::mutate(status_id = duckplyr::if_else(.data$.date_time <= .data$.end_date_time, 3L, .data$status_id, .data$status_id)) |>
duckplyr::mutate(status_id = duckplyr::if_else(.data$status_id != 3L & .data$.date_time <= .data$.end_date_time, 3L, .data$status_id, .data$status_id)) |>
duckplyr::select(!c(".start_date_time", ".end_date_time"))

gap <- data |>
duckplyr::filter(.data$status_id != 1L) |>
duckplyr::mutate(.status_id = pmin(.data$status_id, duckplyr::lead(.data$status_id), na.rm = TRUE),
.status_id = 2L, # 2L, pmin or pmax??
.start_date_time = .data$.date_time,
.end_date_time = duckplyr::lead(.data$.date_time),
.keep = "none") |>
duckplyr::filter(.data$.end_date_time - .data$.start_date_time <= gap_range * 3600)

data |>
dplyr::left_join(gap, by = dplyr::join_by(closest(x$.date_time >= y$.start_date_time))) |>
duckplyr::mutate(status_id = duckplyr::if_else(.data$.date_time <= .data$.end_date_time, .data$.status_id, .data$status_id, .data$status_id)) |>
duckplyr::mutate(status_id = duckplyr::if_else(.data$status_id < .data$.status_id & .data$.date_time <= .data$.end_date_time, .data$.status_id, .data$status_id, .data$status_id)) |>
duckplyr::select(!c(".status_id", ".start_date_time", ".end_date_time")) |>
set_status_id() |>
duckplyr::mutate(.date_time = as.POSIXct(.data$.date_time, tz = tz)) |>
Expand Down
9 changes: 0 additions & 9 deletions tests/testthat/test-classify-time-series-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,6 @@ test_that("additional columns are retained in the output", {
})

test_that("errors when reserved columns are already present in the data", {
expect_error(
classify_time_series_data(data.frame(
date_time = as.POSIXct(c("2021-05-07 08:00:00")),
value = c(4.2),
status_id = c(1L)
)),
regexp = "`colnames\\(data\\)` must not have any values matching 'status_id'."
)

expect_error(
classify_time_series_data(data.frame(
date_time = as.POSIXct(c("2021-05-07 08:00:00")),
Expand Down

0 comments on commit b405e61

Please sign in to comment.