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

latest social care ID update #948

Merged
merged 2 commits into from
May 3, 2024
Merged
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
26 changes: 17 additions & 9 deletions R/replace_sc_id_with_latest.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,40 @@ 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(.data$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"
) %>%
dplyr::filter(!(is.na(.data$period))) %>%
# Overwrite sc id with the latest
dplyr::mutate(
social_care_id = dplyr::if_else(
Expand All @@ -40,6 +49,5 @@ replace_sc_id_with_latest <- function(data) {
.data$social_care_id
)
)

return(return_data)
}