Skip to content

Instantly share code, notes, and snippets.

@geotheory
Created October 2, 2019 16:23
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/71c93412e8091155a5ea3f5f8eed305a to your computer and use it in GitHub Desktop.
Save geotheory/71c93412e8091155a5ea3f5f8eed305a to your computer and use it in GitHub Desktop.
require(tidyverse)
unite2 = function(x, col, ..., sep = '_', remove = TRUE){
unite(x, 'tmpcol', ..., sep = '∞-∞', remove = remove) %>%
mutate(tmpcol = tmpcol %>% str_remove_all('∞-∞NA(?=∞|$)') %>% str_replace_all('∞-∞', sep)) %>%
rename(!!col := tmpcol)
}
x = tibble(x=c('the','a','my'), y=c('brown','cat',NA),z=c('dog',NA,'bird'))
unite2(x, col = 'xzy', c('x','y','z'), sep=' ')
#> # A tibble: 3 x 1
#> xzy
#> <chr>
#> 1 the brown dog
#> 2 a cat
#> 3 my bird
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment