How do I create a new table variable based on the concatenation of two or more categorical variables?
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Steven Lord
2023-1-6
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!