Creating Dummy variables with new variable names for categorical variables

조회 수: 1 (최근 30일)
Sunil Shenoy
Sunil Shenoy 2021년 4월 29일
댓글: Sunil Shenoy 2021년 4월 29일
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