Skip to content

Commit

Permalink
Update replace_sc_id_with_latest function
Browse files Browse the repository at this point in the history
  • Loading branch information
Jennit07 committed Sep 17, 2024
1 parent 1ab7e7f commit 4e3e185
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions R/replace_sc_id_with_latest.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,47 @@ replace_sc_id_with_latest <- function(data) {
# Check for required variables
check_variables_exist(
data,
c("sending_location", "social_care_id", "chi", "latest_flag")
c("sending_location", "social_care_id", "chi", "period")
)

# select variables we need
filter_data <- data %>%
dplyr::select(
"sending_location", "social_care_id", "chi", "latest_flag"
"sending_location", "social_care_id", "chi", "period"
) %>%
dplyr::filter(!(is.na(.data$chi))) %>%
dplyr::distinct()
dplyr::filter(!(is.na(.data$chi)))

change_sc_id <- filter_data %>%
dplyr::filter(latest_flag == 1) %>%
# Sort (by sending_location, chi and period) for unique chi/sending location
dplyr::arrange(
.data$sending_location,
.data$chi,
dplyr::desc(.data$period)
) %>%
# Find the latest sc_id for each chi/sending location by keeping latest period
dplyr::distinct(
.data$sending_location,
.data$chi,
.keep_all = TRUE
) %>%
# Rename for latest sc id
dplyr::rename(latest_sc_id = "social_care_id") %>%
# drop latest_flag for matching
dplyr::select(-"latest_flag")
# drop period for matching
dplyr::select(-"period")

return_data <- change_sc_id %>%
# Match back onto data
dplyr::right_join(data,
by = c("sending_location", "chi"),
multiple = "all"
by = c("sending_location", "chi"),
multiple = "all"
) %>%
dplyr::filter(!(is.na(period))) %>%
# Overwrite sc id with the latest
dplyr::mutate(
social_care_id = dplyr::if_else(
!is.na(.data$chi) & .data$social_care_id != .data$latest_sc_id,
.data$latest_sc_id,
.data$social_care_id
)
) %>%
dplyr::filter(!(is.na(period)))


)
return(return_data)
}

0 comments on commit 4e3e185

Please sign in to comment.