How do I create a new table variable based on the concatenation of two or more categorical variables?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am looking to simply concatenate three variables in a table (2310 rows), where those variables are categorical. I tried:
my_table.new_variable = strcat(my_table.variable1, "_", my_table.variable2, "_", my_table.variable3);
but it does not like the delimiter or the categories, and gave an error.
Error using strcat
Inputs must be character vectors, cell arrays of character vectors, or string arrays.
I tried
a = [my_table.variable1, my_table.variable2, my_table.variable3];
my_table.new_variable = strjoin(a , "_");
but these are categorical and maybe that is why this error (maybe same as above)? a is 2310x3 categorical
Error using strjoin
First input must be a string array or cell array of character vectors.
What I have are three categorical variables that I use for grouping, and I want to create charts and summary statistics by various combinations of the grouping variables. An example would be
my_table.make - [ Ford, Toyota, BMW]
my_table.types - [SUV, Sedan, Compact ]
I want
my_table.configuration - [ Ford_SUV, Ford_Sedan, BMW_Compact, ... etc ].
However, my data set does not have all combinations, which is why I first thought of strcat(). Maybe I am missing a feature in the use of groups in charts and stats.
0 commentaires
Réponses (1)
Steven Lord
le 6 Jan 2023
Do you want these new identifiers (Ford_SUV, Ford_Sedan, BMW_Compact, etc.) to be new categories in a new categorical variable in your table or do you want them to be text labels ("Ford_SUV", "Ford_Sedan", "BMW_Compact", etc.)?
If the latter:
c = categorical(["Red", "Blue", "Green"])
s = string(c(1)) + "_" + string(c(3))
Or for a vector case:
whichColors = randi(numel(c), 5, 3);
sv = string(c(whichColors))
y = join(sv(:, [1 3]), "_")
If the latter it gets a little trickier to work with, depending on what you're trying to do. You won't be able to easily refer to the new categorical array using the categories from the old one even if one of the new categories was created from an instance of that category in the original categorical array.
c2 = categorical(y)
joinedCategories = categories(c2)
originalCategories = categories(c)
y == originalCategories(1)
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!