Pass orderby to dplyr arrange

And a thank you from me @nirgrahamuk. I had this as a side project.

I had wanted to pass in 2 parameters to a function. Each a list of columns and one with the ability to determine sort order.

I don't have test data for a reprex but the gist of it below. DeDup gives unique rows based on indexes (IDs/keys) by sorting based on the order specification in orderfields
The deduplication method is the standard sort and take the first row from each group of identical rows by the ID field.
Example below will take the latest update date with ties decided by create date

DeDup <-  function(dfin , orderfields, indexfields ) {
  dfout <- dfin %>%
    arrange(!!!indexfields, !!!orderfields) %>%
    distinct(!!!indexfields, .keep_all = TRUE)

ExcDups <-  DeDup(IncDups,  
           vars(desc(UpdateDate), desc(CreateDate) ), 
           vars(ID1, ID2))
1 Like