Creating Dummy variables with new variable names for categorical variables
1 次查看(过去 30 天)
显示 更早的评论
I have a dataset with 8 categorical variables "Var1" "Var2"....."Var8"
Each categorical variable has different unique values. I want to create dummy variables for each of the "Var#" variable in this table and these new dummy variables need to be renamed as "Var1_1", "Var1_2" and "Var1_3", assuming Var1 has 3 unique values.
In the Matlab central, there was a post that created a function called "dummytable", but that function did not do what I expected.
I have attached the table in this request. Table name is "AllDataCategorical.mat"
2 个评论
Stephen23
2021-4-29
S = load('AllDataCategorical.mat');
T = S.AllData_Categorical
Please show what the expected output looks like.
回答(1 个)
Jan
2021-4-29
"Var1, Var2, ..." is a bad programming style, because hiding indices in the names of the variables makes it hard, to access them later. Appending a send index like in Var1_1 makes it worse.
Use arrays instead: Var(1), Var(2), ... or maybe cells Var{1}, Var{2}, ... Then it is easy to process the data in loops.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Categorical Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!