Skip to content

Instantly share code, notes, and snippets.

@geotheory
Created September 22, 2021 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geotheory/bf42d0d61730274dd57c77a32f865fd7 to your computer and use it in GitHub Desktop.
Save geotheory/bf42d0d61730274dd57c77a32f865fd7 to your computer and use it in GitHub Desktop.
# replace strings with mode case
require(dplyr)
case_lump = function(x){
x1 = tibble::enframe(x, name=NULL) |> mutate(id = 1:n(), lower = tolower(value))
x1 |> count(value, lower, sort = TRUE) |>
group_by(lower) |> slice(1) |> ungroup() |>
rename(mode = value) |>
left_join(x1, by = 'lower') |>
arrange(id) %>% .[['mode']]
}
# usage
c('xxx','yyy','XXX','YYY','xxx','YYY') |> case_lump()
#> [1] "xxx" "YYY" "xxx" "YYY" "xxx" "YYY"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment