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

23 次查看(过去 30 天)
I have a table with rownames (nuemric value) and want to convert it to a table with a newly added variable from the rownames.

采纳的回答

Star Strider
Star Strider 2023-1-25
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
.

更多回答(0 个)

类别

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

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by