Renaming categories with accents
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Luisa Liboni
el 29 de Nov. de 2018
Respondida: Guillaume
el 30 de Nov. de 2018
I have a categorical array t and some categories can have diacritic/accents, such as circumflexes. I want to standardize everything with no diacritic/accents.
I tried this code:
str = {'Á', 'É', 'Í', 'Ó', 'Ú','Ã','Ç','Â','Ê','Ô'};
strreplace = {'A', 'E', 'I', 'O', 'U','A','C','A','E','O'};
t = categorical({'VÉRDE','VERDE','AZUL','AMARELO','VERMELHO','VERMÊLHO'})';
cat = categories(t);
newcat = cat;
for i = 1:numel(str)
newcat = regexprep(newcat, str{i}, strreplace{i});
end
B = renamecats(t,cat,newcat)
However, after removing the accents, some categories turn out to be the same, for exemple: VERMELHO AND VERMÊLHO.
So I receive the following error:
Error using categorical/renamecats (line 39)
NEWNAMES contains duplicated values.
Is there anyway around?
This is just an example. I need a very efficient code since my categorical array t is comming from a very long table with approximaly 500 categories.
Thanks,
2 comentarios
Jan
el 30 de Nov. de 2018
500 does not sound like big data.
You did not mention what you want to happen, if the names of the categoricals are equal. So it is hard to suggest a solution.
By the way, strrep is much faster than regexprep .
Respuesta aceptada
Guillaume
el 30 de Nov. de 2018
str = {'Á', 'É', 'Í', 'Ó', 'Ú','Ã','Ç','Â','Ê','Ô'};
strreplace = {'A', 'E', 'I', 'O', 'U','A','C','A','E','O'};
t = categorical({'VÉRDE','VERDE','AZUL','AMARELO','VERMELHO','VERMÊLHO'})';
cat = categories(t);
%calculation of new categories, no need for loop
newcat = replace(cat, str, strreplace);
%replace cat by newcat. Create new categorical array using newcat and the index of the original categories in t:
newt = categorical(newcat(double(t)))
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!