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
Stephen23 2021-4-29
S = load('AllDataCategorical.mat');
T = S.AllData_Categorical
T = 297×8 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 ____ ____ ____ ____ ____ ____ ____ ____ 1 0 1 2 0 1 1 0 0 0 0 0 0 0 2 0 0 0 0 0 0 2 0 0 1 0 1 2 0 1 1 0 1 0 0 2 1 1 0 0 1 0 0 2 0 1 0 2 1 0 1 2 0 2 0 1 1 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 1 0 0 2 0 2 0 2 1 0 0 2 0 1 0 2 1 0 0 2 0 0 0 0 1 0 0 0 0 0 2 0 0 0 1 2 0 0 0 0 1 0 0 2 0 1 0 2 1 0 0 2 0 1 0 1
Please show what the expected output looks like.
Sunil Shenoy
Sunil Shenoy 2021-4-29
Thank you Stephen for getting back.
Here is what I will expect for just the Variable Var7. The Var7 has 3 unique values (0,1,2). So the code should create 3 dummy variables Var7_1, Var7_2 and Var7_3.
Then the rows should look like (Just for Var 7).
Var7_1 corresponds to Var7 = 0, Var7_2 corresponds to Var7 =1, Var7_3 corresponds to Var7=2.
So the new table for Var7 will look like:
Var7_1 Var7_2 Var7_3
0 1 0
0 0 1
1 0 0
0 1 0
...
Similarly, there will be additional variables created for Var1, Var2, Var3....
Var1_1 Var1_2
0 1
1 0
1 0
...
Thanks

请先登录,再进行评论。

回答(1 个)

Jan
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.

类别

Help CenterFile Exchange 中查找有关 Categorical Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by