Last active
May 5, 2020 01:23
-
-
Save avallecam/2a98b3a659e3478409fdaabfddf14946 to your computer and use it in GitHub Desktop.
R: Usa janitor::clean_names() //Combo usage of rename_all for spanish useRs :)
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
``` r | |
# prueba ------------------------------------------------------------------ | |
#ejemplo del problema y producto esperado | |
x <- c("êãçàúüñ","EÑE","el camión") | |
iconv(x,to = "ASCII//TRANSLIT") | |
#> [1] "eacauun" "ENE" "el camion" | |
# updated ------------------------------------------------------------------ | |
library(tidyverse) | |
set.seed(22) | |
new_db <- tibble(age = floor(rnorm(6,25,10)), | |
sex = gl(2,1,6, labels = c("f","m")), | |
pop = LETTERS[1:6], | |
dep = c("Áncash","Huánuco","Apurímac",x) | |
) | |
#crear problema | |
#nombre de columnas con "labels" | |
colnames(new_db) <- c("Edad (Años)", | |
"Sexo (Mas/Fem)", | |
"Población", | |
"departamento") | |
new_db | |
#> # A tibble: 6 x 4 | |
#> `Edad (Años)` `Sexo (Mas/Fem)` Población departamento | |
#> <dbl> <fct> <chr> <chr> | |
#> 1 19 f A Áncash | |
#> 2 49 m B Huánuco | |
#> 3 35 f C Apurímac | |
#> 4 27 m D êãçàúüñ | |
#> 5 22 f E EÑE | |
#> 6 43 m F el camión | |
# better alternative ------------------------------------------------------ | |
library(janitor) | |
#> | |
#> Attaching package: 'janitor' | |
#> The following objects are masked from 'package:stats': | |
#> | |
#> chisq.test, fisher.test | |
new_db %>% | |
#clean column names | |
clean_names() %>% | |
#clean vector of names | |
mutate(departamento=make_clean_names(departamento)) %>% | |
#opcional | |
mutate(departamento=str_replace(departamento,"\\_"," ")) | |
#> # A tibble: 6 x 4 | |
#> edad_anos sexo_mas_fem poblacion departamento | |
#> <dbl> <fct> <chr> <chr> | |
#> 1 19 f A ancash | |
#> 2 49 m B huanuco | |
#> 3 35 f C apurimac | |
#> 4 27 m D eacauun | |
#> 5 22 f E ene | |
#> 6 43 m F el camion | |
# old alternative --------------------------------------------------------- | |
#new_db %>% #renombrar | |
# | |
# #a minúsculas | |
# rename_all(funs(str_to_lower(.))) %>% | |
# | |
# #a nombres válidos | |
# rename_all(funs(make.names(.))) %>% | |
# | |
# #los caracteres especiales (eñes o tildes) | |
# rename_all(funs(iconv(.,to = "ASCII//TRANSLIT"))) %>% | |
# | |
# #retirar los puntos (opción 1) | |
# #rename_all(funs(str_replace_all(.,"\\.",""))) | |
# | |
# #conservar solo la primera palabra (opción 2) | |
# rename_all(funs(str_replace_all(.,"\\.(.+)",""))) | |
# | |
# tibble(z=c(x,y)) %>% | |
# mutate(t1=iconv(x = z,to = "ASCII//TRANSLIT"), | |
# t2=iconv(x = z,to = "ASCII"), | |
# t3=iconv(x = z,to = "ASCII",sub = "N"), | |
# t4=janitor::make_clean_names(z)) | |
# | |
``` | |
<sup>Created on 2019-08-07 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0)</sup> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment