Created
September 22, 2021 12:46
-
-
Save geotheory/bf42d0d61730274dd57c77a32f865fd7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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