How to batch convert table columns to categorical

24 vues (au cours des 30 derniers jours)
Giancarlo Storti Gajani
Giancarlo Storti Gajani le 19 Sep 2018
I have a large table (large number of columns and very large number of rows), many of these columns contain simple cell arrays with a very limited number of different options (such as 'ON" and 'OFF' or similar).
I would like to convert all these columns to categorical in an automated way, i.e. extracting column names from
Table.Properties.VariableNames
and not explicitly writing the (large) number of column names in dot notation.
I have seen that
>> ddd2 = categorical(MyTable{:,MyTable.Properties.VariableNames(1)});
works fine (i.e. ddd2 is a correct categorical array), but, at this point, I cannot put this categorical array back in my table. I tried:
>> MyTable{:,MyTable.Properties.VariableNames{1}}= ddd2;
The following error occurred converting from categorical to cell:
Conversion to cell from categorical is not possible.
but it returns the error you see above... On the other hand, if I use dot notation it works fine, i.e.
MyTable.Column1 = ddd2;
yields the desired results. I found a workaround, that works perfectly, but I would like to know if there is a more direct and elegant way to solve this problem.... this is the workaround:
sz0=size(MyTable);
kk=0;
for( i=1:sz0(2) )
if( iscell(MyTable{:,i}) )
ddd2 = categorical(MyTable{:,MyTable.Properties.VariableNames(i)});
eval(sprintf('MyTable.%s = ddd2;',MyTable.Properties.VariableNames{i}));
kk = kk+1;
end
end
fprintf( ' %d columns transformed to categorical\n', kk );
it works...
  1 commentaire
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco le 6 Juil 2021
Dear Giancarlo,
thanks for your tip on including the Table.Properties.VariableNames in a for loop. You know, it was the easy to create a cell array for the 'vars' input of the change variable types built-in function.
Best.

Connectez-vous pour commenter.

Réponse acceptée

Sean de Wolski
Sean de Wolski le 19 Sep 2018
In 18b there is a convenience function to do exactly this:
Note, that it may be possible to just create your table with categoricals. How are you creating the table?
  7 commentaires
Gwendolyn Williams
Gwendolyn Williams le 27 Fév 2021
I have the same error, and I'm inputting a table into convertvars.
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco le 6 Juil 2021
Dear Sean,
thank you. You reminded me of the built-in function, best!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by