The code below works perfectly fine and outputs the data of interest. However, I am wondering if there is a better solution or different way think about the logic.
Essentially, I need filter for the updated_dt (date time) based on the grouping for only the 717 aircraft and pilots (first part of the code). All the other combination of aircrafts and pilots or flight attendants does not need to change.
Just seems like there should be a smoother process and it looks like there may be many edge cases where I need to apply operations on specific groups (pilors or FAs) per aircrast time.
int_717_p <- int_prob_final_pairing %>%
filter(EQUIPMENT == "717",
CREW_INDICATOR == "P") %>%
group_by(PAIRING_POSITION, PAIRING_NO, DEPARTING_CITY, ARRIVAL_CITY, SCHED_DEPARTURE_DATE, SCHED_DEPARTURE_TIME, PAIRING_DATE) %>%
filter(updated_dt == max(updated_dt))
all_other_carft <- int_prob_final_pairing %>%
filter(!EQUIPMENT == "717")
fa_717 <- int_prob_final_pairing %>%
filter(EQUIPMENT == "717",
CREW_INDICATOR == "FA")
final_pairing <- rbind(int_717_p, all_other_carft, fa_717)
1
what about:
final_pairing <-
int_prob_final_pairing |>
mutate(is_max_updated_dt = updated_dt == max(updated_dt, na.rm = TRUE),
.by = c(PAIRING_POSITION, PAIRING_NO) ## add other grouping vars
) |>
filter(
(EQUIPMENT != '717') |
(EQUIPMENT == '717' & CREW_INDICATOR == "FA") |
(EQUIPMENT == '717' & CREW_INDICATOR == 'P' & is_max_updated_dt)
) |>
select(-is_max_updated_dt) ## drop helper column
Note: as you didn’t provide sample data I could not test the code. However, it demonstrates how to combine all steps into a single pipeline without populating the workspace with temporary objects.