Skip to content

Commit

Permalink
Fix get_repeat_event_types edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
rsh52 committed Sep 20, 2024
1 parent 374ad48 commit 37b88a8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 3 additions & 2 deletions R/read_redcap.R
Original file line number Diff line number Diff line change
Expand Up @@ -544,10 +544,11 @@ get_repeat_event_types <- function(data) {

# Check for instances where the same event is labelled as nonrepeating & repeating separate
# If this is the case, it must be repeating separate (there is just data that qualifies as both)

out %>%
mutate(
is_duplicated = duplicated(.data$repeat_type) | duplicated(.data$repeat_type, fromLast = TRUE)
is_duplicated = (duplicated(.data$redcap_event_name) | duplicated(.data$redcap_event_name, fromLast = TRUE))
) %>%
filter(.data$is_duplicated == FALSE | (.data$is_duplicated == TRUE & .data$ repeat_type == "repeat_separate")) %>%
filter(!.data$is_duplicated | (.data$is_duplicated & .data$repeat_type == "repeat_separate")) %>%
select(-.data$is_duplicated)
}
21 changes: 21 additions & 0 deletions tests/testthat/test-read_redcap.R
Original file line number Diff line number Diff line change
Expand Up @@ -660,4 +660,25 @@ test_that("get_repeat_event_types() works", {
out <- get_repeat_event_types(mixed_data_structure)

expect_equal(out, expected_out)

# Example with nonrepeating arm that contains repeating and non repeating forms
mixed_data_structure <- tibble::tribble(
~"record_id", ~"redcap_event_name", ~"redcap_repeat_instrument", ~"redcap_repeat_instance",
1, "nonrepeat", NA, NA,
1, "nonrepeat", "repeat_form", 1,
1, "repeat_together", NA, 1,
1, "repeat_separate", "mixed_structure_form", 1
)

out <- get_repeat_event_types(mixed_data_structure)

expected_out <- tibble::tribble(
~"redcap_event_name", ~"repeat_type",
"nonrepeat", "repeat_separate",
"repeat_together", "repeat_together",
"repeat_separate", "repeat_separate"
)

expect_equal(out, expected_out)

})

0 comments on commit 37b88a8

Please sign in to comment.