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

Create update_cols function #73

Closed
sfirke opened this issue Oct 20, 2016 · 2 comments
Closed

Create update_cols function #73

sfirke opened this issue Oct 20, 2016 · 2 comments

Comments

@sfirke
Copy link
Owner

sfirke commented Oct 20, 2016

Supply a main data.frame and one with a join-by ID as well as columns sharing names of the main DF. This swaps in values in the auxiliary data.frame, but leaves the original values in place if there's no new one to overwrite with.

Not sure how widespread this is, have to weigh popularity against bloating the package. But I had to do a lot of this recently and have a sketch of the code already:

function(x) {
  left_join(x, aux_df, by = c("TT2 ID")) %>%
    mutate(`ACE Status` = use_first_valid_of(`ACE Status.y`, `ACE Status.x`),
           `Certification Status` = use_first_valid_of(`Certification Status.y`, `Certification Status.x`),
           `Date Certified/Extended/Removed` = use_first_valid_of(`Date Certified/Extended/Removed.y`, `Date Certified/Extended/Removed.x`),
           `ETS Status` = use_first_valid_of(`ETS Status.y`, `ETS Status.x`)) %>%
    select(-contains(".")) %>% # drop the .x and .y overlapping variables
    select(one_of(original_col_order))
}
@rgknight
Copy link
Collaborator

This is an upsert, more or less. I'd like to see it in "base" dplyr. Maybe chime in on the dplyr issue. tidyverse/dplyr#2075

@sfirke
Copy link
Owner Author

sfirke commented May 5, 2017

Yep @rgknight is correct, if tidyr adds an upsert like discussed here it'll likely be better than whatever we'd do. Closing this, keep an eye on tidyverse/tidyr#183.

@sfirke sfirke closed this as completed May 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants