How to covert rownames to the first variable in the table?

54 views (last 30 days)
I have a table with rownames (nuemric value) and want to convert it to a table with a newly added variable from the rownames.

Accepted Answer

Star Strider
Star Strider on 25 Jan 2023
Try something like this —
T1 = array2table(randi(9, 5, 5));
T1.Properties.RowNames = {'1','2','3','4','5'} % Create Table With Row Names
T1 = 5×5 table
Var1 Var2 Var3 Var4 Var5 ____ ____ ____ ____ ____ 1 2 8 8 8 3 2 1 3 8 8 7 3 5 8 5 5 7 4 9 3 1 2 1 5 2 5 1 7 2
RN = T1.Properties.RowNames % Get Row Names
RN = 5×1 cell array
{'1'} {'2'} {'3'} {'4'} {'5'}
T1 = addvars(T1, cell2mat(RN), 'Before','Var1') % Convert Row Names To First Variable
T1 = 5×6 table
Var1_1 Var1 Var2 Var3 Var4 Var5 ______ ____ ____ ____ ____ ____ 1 1 2 8 8 8 3 2 2 1 3 8 8 7 3 3 5 8 5 5 7 4 4 9 3 1 2 1 5 5 2 5 1 7 2
T1.Properties.RowNames = {} % Delete Original Row Names
T1 = 5×6 table
Var1_1 Var1 Var2 Var3 Var4 Var5 ______ ____ ____ ____ ____ ____ 1 2 8 8 8 3 2 1 3 8 8 7 3 5 8 5 5 7 4 9 3 1 2 1 5 2 5 1 7 2
.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!